count secton of data in list

odeits odeits at gmail.com
Sat Feb 21 21:34:47 EST 2009


On Feb 20, 3:45 pm, Emile van Sebille <em... at fenx.com> wrote:
> brianrpsgt1 wrote:
>
> > def step1(val):
>
>        data2_row = []
>
> >     for d1r in data1_row:
> >         if d1r[1] >= val:
> >             switch = 0
> >             data2_row = d1r[0],d1r[1],d1r[2],switch
>
>                data2_row.append([d1r[0],d1r[1],d1r[2],switch])
>
> HTH,
>
> Emile

def count_consecutive(rows):
    switch = 0
    count = 0
    for r in rows:
        if r[-1] == switch:
            count += 1
        else:
            switch = not switch
            if count != 0:
                yield count
                count = 0
    if count != 0:
        yield count



rows = [
['2009-01-09','13:17:30,96',123456,0],
['2009-01-09','13:17:31,95',123456,0],
['2009-01-09','13:17:32,95',123456,0],
['2009-01-09','13:17:33,95',123456,0],
['2009-01-09','13:17:34,94',123456,1],
['2009-01-09','13:17:35,94',123456,1],
['2009-01-09','13:17:36,94',123456,1],
['2009-01-09','13:17:37,94',123456,1],
['2009-01-09','13:17:38,94',123456,1],
['2009-01-09','13:17:39,94',123456,1],
['2009-01-09','13:17:40,94',123456,1],
['2009-01-09','13:17:41,94',123456,1],
['2009-01-09','13:17:42,95',123456,0],
['2009-01-09','13:17:43,95',123456,0],
['2009-01-09','13:17:44,95',123456,0],
['2009-01-09','13:17:45,95',123456,0]
]

for cnt in count_consecutive(rows):
    print cnt



More information about the Python-list mailing list