Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30506 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2012-09-29 10:36 -0600 |
| Last post | 2012-09-29 10:36 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Slicing iterables in sub-generators without loosing elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-29 10:36 -0600
| 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 |
| Message-ID | <mailman.1638.1348936632.27098.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web