开发者

Counting successive occurrences of numbers satisfying certain conditions in python

I have a specific data set that reads like

array([   2.1,    3. ,  179.1,    1.9,    2.6,  425.8,    1.7,    3.1,
          4. ,  144. ,    2.2,    2.3,    5.3,  135.5,    2. ,    2.7,
      .....])]

Here I want to count the successive occurrences of numbers below 6 and save them in specific counters. For example, in the first three numbers there are two numbers that are continuously below 6 before a bigger number appears. So counter2 would get an addition of 1. If three numbers occur continuously like that, then counter3 would be incremented by 1 (as in the 2nd row) and so on. Are there any functions to do it i开发者_运维问答n python, if not how do I proceed? Thanks in advance.


Here's my solution, based on my understanding of your problem:

from collections import defaultdict

def countSuccessive(data):
    counters = defaultdict(int)
    count = 0

    for i in data:
        if i < 6:
            count += 1
        elif count != 0:
            counters[count] += 1
            count = 0

    if count != 0:
        counters[count] += 1

    return counters

result = countSuccessive([
    2.1,    3. ,  179.1,    1.9,    2.6,  425.8,    1.7,    3.1,
    4. ,  144. ,    2.2,    2.3,    5.3,  135.5,    2. ,    2.7])

print repr(result)

Output:

defaultdict(<type 'int'>, {2: 3, 3: 2})

The counters are result[2] and result[3] in this case. You can inspect the dict to see which keys exist.


import numpy as np
import itertools as it

a = np.array([   2.1,    3. ,  179.1,    1.9,
                 2.6,  425.8,    1.7,    3.1,
                 4. ,  144. ,    2.2,    2.3,
                 5.3,  135.5,    2. ,    2.7])

counters = {}
for grp in (len(list(n)) for t,n in it.groupby(a>6) if not t):
    counters[grp] = counters.get(grp, 0) + 1

# counters: {2: 3, 3: 2}, i.e. counter2 = 3, counter3 = 2

or, if you want just your counter2 variable:

counter2 = sum(1 for t,n in it.groupby(a>6) if not t and len(list(n)) == 2)


This might work.

def f(data):
    counters = {}
    succ = 0
    for item in data:
        if item < 6:
            succ += 1
        elif succ > 0:
            try:
                counters[succ] += 1
            except KeyError:
                counters[succ] = 1
            succ = 0
    if succ > 0:
        try:
            counters[succ] += 1
        except KeyError:
            counters[succ] = 1        
    return counters

It returns a dictionary where keys are number of successive numbers lower than 6 and value is number of such occurences. I might not understood it properly. If so, correct me.

edit: Meanwhile cdhowie posted similiar answer which got rid of try/except.


My solution

from collections import Counter
from itertools import groupby

l = [2.1, 3., 179.1, 1.9, 2.6, 425.8, 1.7, 3.1,
     4.,  144., 2.2, 2.3, 5.3, 135.5, 2., 2.7]

lengths = [len(list(g)) for (k, g) in groupby(l, key = lambda x: x < 6) if k]
print Counter(lengths)

If you don't have Python 2.7 which has the Counter class you can use a defaultdict instead:

d = defaultdict(int)
for el in lengths: 
    d[el] += 1
print d
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜