Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30503

Slicing iterables in sub-generators without loosing elements

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <thbach@students.uni-mainz.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.033
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; '3),': 0.09; 'def': 0.10; '1),': 0.16; '2),': 0.16; '2)]': 0.16; 'entry:': 0.16; 'evaluates': 0.16; 'intuition': 0.16; 'lambda': 0.16; 'subject:sub': 0.16; 'true:': 0.16; 'element': 0.17; 'yield': 0.17; '>>>': 0.18; 'elements': 0.23; 'pass': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.28; 'e.g.': 0.30; 'function': 0.30; 'code': 0.31; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'entry': 0.33; 'list': 0.35; 'something': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'does': 0.37; 'data': 0.37; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'content-disposition:inline': 0.60; 'first': 0.61; 'back': 0.62; 'thomas': 0.62; 'skip:n 10': 0.63; 'sharing': 0.74; 'received:10.94': 0.84; 'solvable': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1348935353; x=1380471353; h=date:from:to:subject:message-id:mime-version; bh=2K4kF4R8jxbob+fkDTByao5dyVx5LZyl5ijWzCZoBfQ=; b=JKTYtAf+aVwaSXkjTz3Qc+kfllwvgVpGCeAf8hei+F1dDPFcorQh2DBQ mFSDhznQtTKhC31VbvwcIcTbXFCE9lGw9AfFER8GNXZsIf2n08l+XQueH /gA4gf3GS2TnVezR/xH+EHg7MznoVelQGPuXK0ic04atcWmeZbq9xNvqL g=;
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result ApwEAIwdZ1AKXgZQ/2dsb2JhbABFvziCIAEBBDuBECQpIIgSCph1oQmOSoJAYAOVaAGQK4JpghU
Date Sat, 29 Sep 2012 18:14:38 +0200
From Thomas Bach <thbach@students.uni-mainz.de>
To <python-list@python.org>
Subject Slicing iterables in sub-generators without loosing elements
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Disposition inline
User-Agent Mutt/1.5.21 (2010-09-15)
X-Originating-IP [94.219.181.175]
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1636.1348935354.27098.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348935354 news.xs4all.nl 6900 [2001:888:2000:d::a6]:35097
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30503

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Slicing iterables in sub-generators without loosing elements Thomas Bach <thbach@students.uni-mainz.de> - 2012-09-29 18:14 +0200
  Re: Slicing iterables in sub-generators without loosing elements Paul Rubin <no.email@nospam.invalid> - 2012-09-29 09:26 -0700
    Re: Slicing iterables in sub-generators without loosing elements Thomas Bach <thbach@students.uni-mainz.de> - 2012-09-29 18:44 +0200
  Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-30 17:58 -0700
    Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-01 09:19 +0100
      Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:12 -0700
        Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-02 17:44 +0100
          Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 20:21 -0700
          Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 20:21 -0700
        Re: Slicing iterables in sub-generators without loosing elements Chris Angelico <rosuav@gmail.com> - 2012-10-03 03:58 +1000
          Re: Slicing iterables in sub-generators without loosing elements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-03 00:57 +0000
            Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 18:11 -0700
              Re: Slicing iterables in sub-generators without loosing elements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-03 01:24 +0000
                Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 18:42 -0700
        Re: Slicing iterables in sub-generators without loosing elements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-02 19:35 +0100
        Re: Slicing iterables in sub-generators without loosing elements Terry Reedy <tjreedy@udel.edu> - 2012-10-02 14:59 -0400
      Re: Slicing iterables in sub-generators without loosing elements Ramchandra Apte <maniandram01@gmail.com> - 2012-10-02 09:12 -0700
      Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 13:54 -0700
      Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-02 13:54 -0700
  Re: Slicing iterables in sub-generators without loosing elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-30 17:58 -0700

csiph-web