count secton of data in list

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

  1. brianrpsgt1

    brianrpsgt1 Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. brianrpsgt1

    odeits Guest

    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
    #3
  4. brianrpsgt1

    S Arrowsmith Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    892
    Natty Gur
    Nov 3, 2003
  2. Amy G
    Replies:
    9
    Views:
    311
    Peter Otten
    Nov 24, 2003
  3. Bart Nessux
    Replies:
    4
    Views:
    398
    Tim Roberts
    Jan 10, 2004
  4. Johan Hahn

    list.count() with no arguments

    Johan Hahn, Mar 27, 2005, in forum: Python
    Replies:
    1
    Views:
    368
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 27, 2005
  5. efelnavarro09
    Replies:
    2
    Views:
    900
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page