count secton of data in list

Discussion in 'Python' started by brianrpsgt1, Feb 20, 2009.

1. brianrpsgt1Guest

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

brianrpsgt1, Feb 20, 2009

2. Emile van SebilleGuest

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

Emile van Sebille, Feb 20, 2009

3. odeitsGuest

On Feb 20, 3:45 pm, Emile van Sebille <> 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

odeits, Feb 22, 2009
4. S ArrowsmithGuest

In article <>,
odeits <> wrote:
>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))

--
\S

under construction

S Arrowsmith, Feb 23, 2009