Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30579
| 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 | <dihedral88888@googlemail.com> |
| 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 | <mailman.1636.1348935354.27098.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw |
| References | <mailman.1636.1348935354.27098.python-list@python.org> |
| 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 <dihedral88888@googlemail.com> |
| 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 <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> |
| Message-ID | <mailman.1687.1349053129.27098.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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