count secton of data in list

B

brianrpsgt1

I have a list of three columns of data. I run the following code:

def step1(val):
for d1r in data1_row:
if d1r[1] >= val:
switch = 0
data2_row = d1r[0],d1r[1],d1r[2],switch
print d1r[0],d1r[1],d1r[2],switch
else:
switch = 1
print d1r[0],d1r[1],d1r[2],switch

step1(95)

After running that code I get four columns with either a '0' or '1' in
the 4th column, as shown below

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

Where I am getting stuck is that I now need to get the individual
counts for the various consecutive areas in the list where the values
are '1'. I was trying to run a FOR Loop on the variable data2_row....
but that does not work. Any assistance would be great.

Thanks:
B
 
O

odeits

brianrpsgt1 said:
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
 
S

S Arrowsmith

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

rows = [ ... ]

for cnt in count_consecutive(rows):
print cnt

import itertools, operator

for k, g in itertools.groupby(rows, operator.itemgetter(3):
print len(list(g))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top