Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'data:': 0.07; '3),': 0.09; 'immutable': 0.09; 'iterate': 0.09; 'to:addr:comp.lang.python': 0.09; 'tuple': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; '1),': 0.16; '2),': 0.16; '2)]': 0.16; 'entry:': 0.16; 'evaluates': 0.16; 'intuition': 0.16; 'lambda': 0.16; 'simple.': 0.16; 'storing': 0.16; 'subject:sub': 0.16; 'true:': 0.16; 'x1,': 0.16; 'wrote:': 0.17; 'element': 0.17; 'yield': 0.17; '>>>': 0.18; 'tuples': 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'question': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'e.g.': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'problem': 0.33; 'hi,': 0.33; 'entry': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'from:addr:googlemail.com': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'does': 0.37; 'item': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'subject:-': 0.40; 'your': 0.60; 'first': 0.61; 'back': 0.62; '30,': 0.62; 'thomas': 0.62; 'skip:n 10': 0.63; 'sharing': 0.74; 'solvable': 0.84; 'received:209.85.212.56': 0.91 Newsgroups: comp.lang.python Date: Sun, 30 Sep 2012 17:58:39 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: Slicing iterables in sub-generators without loosing elements From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 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: , Message-ID: Lines: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349053129 news.xs4all.nl 6969 [2001:888:2000:d::a6]:40622 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30579 On Sunday, September 30, 2012 12:15:57 AM UTC+8, Thomas Bach wrote: > Hi, > > > > say we have the following: > > > > >>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)] > > > > is there a way to code a function iter_in_blocks such that > > > > >>> result = [ list(block) for block in iter_in_blocks(data) ] > > > > evaluates to > > > > >>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ] > > > > by _only_ _iterating_ over the list (caching all the elements sharing > > the same first element doesn't count)? > > > > I came up with the following > > > > def iter_in_blocks(iterable): > > my_iter = iter(iterable) > > while True: > > first = next(my_iter) > > pred = lambda entry: entry[0] == first[0] > > def block_iter(): > > yield first > > for entry in itertools.takewhile(pred, my_iter): > > yield entry > > yield block_iter() > > > > which does not work as itertools.takewhile consumes the first entry > > not fulfilling the pred. > > > > I currently have the intuition that the problem is not solvable > > without using e.g. a global to pass something back to iter_in_blocks > > from block_iter. Any other suggestions? > > > > Regards, > > Thomas Bach. Your question seems vague to me. If you know you are storing only immutable tuples in a list, then the way to iterate is simple. For example: data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)] # all tuples for item in data: x1=item[0] # first entry in each tuple x2=item[1] print x1, x2 # or use yield in a function to iterate