Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'output': 0.05; 'represents': 0.05; '40,': 0.09; '[1,': 0.09; 'correct,': 0.09; 'data:': 0.09; 'cc:addr:python-list': 0.11; '#this': 0.16; '#we': 0.16; '60,': 0.16; 'appreciated!': 0.16; 'count.': 0.16; 'index.': 0.16; 'pythonic': 0.16; 'subject:broken': 0.16; 'subject:make': 0.16; 'tup': 0.16; 'tuple': 0.16; 'tuple.': 0.16; '\xa0this': 0.16; 'index': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'thanks.': 0.20; '(the': 0.22; '>>>': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '>>>': 0.24; 'replace': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'points': 0.29; 'have,': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'index,': 0.31; 'figure': 0.32; "we're": 0.32; 'url:python': 0.33; 'alone': 0.33; 'skip:t 40': 0.33; 'sense': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'same.': 0.38; 'track': 0.38; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'break': 0.61; 'new': 0.61; 'matter': 0.61; 'times': 0.62; 'more': 0.64; 'by:': 0.65; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'here': 0.66; 'between': 0.67; '20,': 0.68; 'therefore': 0.72; 'behavioral': 0.74; 'jul': 0.74; 'trial': 0.83; 'occasion': 0.84; '20th': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=isG9cdcaOU1BomDmkYqTqpHxjpS3w1e0bSx74BsCJTI=; b=jU84E/nPjcHmRQ8rhGJ0fNsSW1XNrzewqGlZE1k/fTrwqCRLU+ZVw6B39Gy/E/Gq9/ HHOXQG38Fdf4/dIr3ZKrn1SmY/CVJd5yYGNy46YL1zvzDA5Nv/T6u9OdBLJo3sB+Xczq bg8BLb3VpAw/eKW+f+/REvwptfxR8+G50L5+YrrcSQU4KXSLZCSFrRC0sFZ+wM3gkLTj 7W1ldd87by6UvUHAUZvs92qiGSLFaTyTbNmj/xbiVO4kUpoeJBjO3CJrVUTgO7bnzh0I mPZ8YcL8ULgsB/HWtlTGnLLd2XAl6gMSixlHEIH58y6jYU7lNjJ9d5LJcWmj9rRBwRcR A1zQ== MIME-Version: 1.0 X-Received: by 10.49.82.234 with SMTP id l10mr17682096qey.17.1373318015649; Mon, 08 Jul 2013 14:13:35 -0700 (PDT) In-Reply-To: <9d0cd072-3cf7-4156-8e84-884faeef7048@googlegroups.com> References: <9d0cd072-3cf7-4156-8e84-884faeef7048@googlegroups.com> Date: Mon, 8 Jul 2013 22:13:35 +0100 Subject: Re: make sublists of a list broken at nth certain list items From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: CM Content-Type: multipart/alternative; boundary=047d7b6743ea7c8e9e04e10684f7 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 177 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373318330 news.xs4all.nl 15929 [2001:888:2000:d::a6]:44224 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50174 --047d7b6743ea7c8e9e04e10684f7 Content-Type: text/plain; charset=ISO-8859-1 You don't want to use index() to figure out the index of the tuples. It is slower, and will not find the item you want if there is more than one of the same. For example, [1, 4, 4, 4].index(4) will always be 1, no matter how many times you loop through it. Instead, use enumerate() to keep track of the index. Replace your loop by: for index, tup in enumerate(data_list): This should fix your problem. After you have the correct indices, look into list slicing syntax. On 8 Jul 2013 21:59, "CM" wrote: > I'm looking for a Pythonic way to do the following: > > I have data in the form of a long list of tuples. I would like to break > that list into four sub-lists. The break points would be based on the nth > occasion of a particular tuple. (The list represents behavioral data > trials; the particular tuple represents the break between trials; I want to > collect 20 trials at a time, so every 20th break between trials, start a > new sublist). > > So say I have this data: > > data_list = [(0.0, 1.0), (1.0, 24.0), (24.0, 9.0), (9.0, 17.0), (17.0, > 5.0), (5.0, 0.0), (5.0, 0.0), (5.0, 24.0), (24.0, 13.0), (13.0, 0.0), > (13.0, 21.0), (21.0, 0.0), (21.0, 0.0), (21.0, 23.0), (23.0, 24.0), (24.0, > 10.0), (10.0, 18.0), (18.0, 4.0), (4.0, 22.0), (22.0, 1.0), (1.0, 0.0), > (1.0, 24.0), (24.0, 6.0), (6.0, 14.0), (14.0, 5.0), (5.0, 0.0), (5.0, 0.0), > (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), > (5.0, 0.0), (5.0, 24.0), (24.0, 6.0), (6.0, 14.0), (14.0, 4.0), (4.0, 0.0), > (4.0, 22.0), (22.0, 1.0), (1.0, 0.0), (1.0, 24.0), (24.0, 9.0), (9.0, > 17.0), (17.0, 4.0), (4.0, 0.0), (4.0, 22.0), (22.0, 1.0), (1.0, 0.0), (1.0, > 0.0), (1.0, 24.0), (24.0, 12.0), (12.0, 4.0), (4.0, 0.0), (4.0, 22.0)] > #rest of data truncated... > > I'd like to break the list into sublists at the 20th, 40th, and 60th > occasions of any tuple that begins with 1.0--so for example, (1.0, 0.0). > This will produce four sub-lists, for trial 1-20, 21-40, 41-60, and 61-80. > > What I have, just to get the break points within the data_list, and which > is not working is: > > trial_break_indexes_list = [] #needed to see where the sublists start > trial_count = 0 #keep count of which trial we're on > > trial_break_indexes_list = [] #holds the index of the transitions_list > for trials 1-20, 21-40, 41-60, and 61-80 > trial_count = 0 > > for tup in data_list: > if tup[0] == 1.0: #Therefore the start of a new trial > > #We have a match! Therefore get the index in the data_list > data_list_index = data_list.index(tup) > > trial_count += 1 #update the trial count. > > if trial_count % 20 == 0: #this will match on 0, 20, 40, 60, 80 > trial_break_indexes_list.append(data_list_index) > > print 'This is trial_break_indexes_list: ', trial_break_indexes_list > > Unfortunately, the final output here is: > > >>> > This is trial_break_indexes_list: [1, 20, 20, 20, 20, 1, 20, 1] > > I sense there is a way more elegant/simpler/Pythonic way to approach this, > let alone one that is actually correct, but I don't know of it. > Suggestions appreciated! > > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > --047d7b6743ea7c8e9e04e10684f7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

You don't want to use index() to figure out the index of= the tuples. It is slower, and will not find the item you want if there is = more than one of the same. For example,

[1, 4, 4, 4].index(4)

will always be 1, no matter how many times you loop through = it.

Instead, use enumerate() to keep track of the index. Replace= your loop by:

for index, tup in enumerate(data_list):

This should fix your problem. After you have the correct ind= ices, look into list slicing syntax.

On 8 Jul 2013 21:59, "CM" <cmpython@gmail.com> wrote:
I'm looking for a Pythonic way to do the following:

I have data in the form of a long list of tuples. =A0I would like to break = that list into four sub-lists. =A0The break points would be based on the nt= h occasion of a particular tuple. =A0(The list represents behavioral data t= rials; the particular tuple represents the break between trials; I want to = collect 20 trials at a time, so every 20th break between trials, start a ne= w sublist).

So say I have this data:

data_list =3D [(0.0, 1.0), (1.0, 24.0), (24.0, 9.0), (9.0, 17.0), (17.0, 5.= 0), (5.0, 0.0), (5.0, 0.0), (5.0, 24.0), (24.0, 13.0), (13.0, 0.0), (13.0, = 21.0), (21.0, 0.0), (21.0, 0.0), (21.0, 23.0), (23.0, 24.0), (24.0, 10.0), = (10.0, 18.0), (18.0, 4.0), (4.0, 22.0), (22.0, 1.0), (1.0, 0.0), (1.0, 24.0= ), (24.0, 6.0), (6.0, 14.0), (14.0, 5.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0= ), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), (5.0, 0.0), = (5.0, 24.0), (24.0, 6.0), (6.0, 14.0), (14.0, 4.0), (4.0, 0.0), (4.0, 22.0)= , (22.0, 1.0), (1.0, 0.0), (1.0, 24.0), (24.0, 9.0), (9.0, 17.0), (17.0, 4.= 0), (4.0, 0.0), (4.0, 22.0), (22.0, 1.0), (1.0, 0.0), (1.0, 0.0), (1.0, 24.= 0), (24.0, 12.0), (12.0, 4.0), (4.0, 0.0), (4.0, 22.0)] =A0#rest of data tr= uncated...

I'd like to break the list into sublists at the 20th, 40th, and 60th oc= casions of any tuple that begins with 1.0--so for example, (1.0, 0.0). =A0T= his will produce four sub-lists, for trial 1-20, 21-40, 41-60, and 61-80.
What I have, just to get the break points within the data_list, and which i= s not working is:

trial_break_indexes_list =3D [] =A0#needed to see where the sublists start<= br> trial_count =3D 0 =A0#keep count of which trial we're on

trial_break_indexes_list =3D [] =A0#holds the index of the transitions_list= for trials 1-20, 21-40, 41-60, and 61-80
trial_count =3D 0

for tup in data_list:
=A0 =A0 if tup[0] =3D=3D 1.0: #Therefore the start of a new trial

=A0 =A0 =A0 =A0 #We have a match! =A0Therefore get the index in the data_li= st
=A0 =A0 =A0 =A0 data_list_index =3D data_list.index(tup)

=A0 =A0 =A0 =A0 trial_count +=3D 1 =A0#update the trial count.

=A0 =A0 =A0 =A0 if trial_count % 20 =3D=3D 0: =A0#this will match on 0, 20,= 40, 60, 80
=A0 =A0 =A0 =A0 =A0 =A0 trial_break_indexes_list.append(data_list_index)
print 'This is trial_break_indexes_list: ', trial_break_indexes_lis= t

Unfortunately, the final output here is:

>>>
This is trial_break_indexes_list: =A0[1, 20, 20, 20, 20, 1, 20, 1]

I sense there is a way more elegant/simpler/Pythonic way to approach this, = let alone one that is actually correct, but I don't know of it. =A0Sugg= estions appreciated!

Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
--047d7b6743ea7c8e9e04e10684f7--