Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30506
| References | <20120929161437.GA8832@taris.box> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-09-29 10:36 -0600 |
| Subject | Re: Slicing iterables in sub-generators without loosing elements |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1638.1348936632.27098.python-list@python.org> (permalink) |
On Sat, Sep 29, 2012 at 10:14 AM, Thomas Bach
<thbach@students.uni-mainz.de> 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)?
Am I correct in understanding that the intent is that the "blocks" are
groups that share the same first item?
Is it guaranteed that the blocks will be contiguous? If so, you could
use itertools.groupby:
from itertools import groupby, imap
from operator import itemgetter
def iter_in_blocks(data):
return imap(itemgetter(1), groupby(data, itemgetter(0)))
If there is no such guarantee, then the list would need to be sorted first.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Slicing iterables in sub-generators without loosing elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-29 10:36 -0600
csiph-web