On Fri, 17 Dec 2021 09:25:03 +0100, ast wrote:
[snip]
>
> but:
>
> li = [grp for k, grp in groupby("aahfffddnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand why, this is
> the first read of an iterator, it should provide its
> data.
Baffling. Here'
On Sat, Dec 18, 2021 at 2:32 AM ast wrote:
>
> Python 3.9.9
>
> Hello
>
> I have some troubles with groupby from itertools
>
> from itertools import groupby
>
> li = [grp for k, grp in groupby("aahfffddnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand
but:
li = [grp for k, grp in groupby("aahfffddnnb")]
list(li[0])
[]
list(li[1])
[]
It seems empty ... I don't understand why, this is
the first read of an iterator, it should provide its
data.
The group-iterators are connected. Each group-iterator is a wrapper
around the original it
Python 3.9.9
Hello
I have some troubles with groupby from itertools
from itertools import groupby
for k, grp in groupby("aahfffddnnb"):
print(k, list(grp))
print(k, list(grp))
a ['a', 'a']
a []
h ['h']
h []
f ['f', 'f', 'f']
f []
d ['d', 'd']
d []
s ['s', 's', 's', 's']
s []
n ['n
> itertools only looks for changes to the key value (the one returned by
> operator.itemgetter(0) in your case); it doesn't sort the list for you.
>
> this should work:
>
>for k, g in itertools.groupby(sorted(vals), operator.itemgetter(0)):
> print k, [i for i in g]
footnote: to turn
[EMAIL PROTECTED] wrote:
> What am I doing wrong here?
>
import operator
import itertools
vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),
> ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)]
for k, g in itertools.groupby(iter(vals), operator.itemgetter(0)):
> ... p
[EMAIL PROTECTED] wrote:
> What am I doing wrong here?
>
import operator
import itertools
vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),
> ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)]
for k, g in itertools.groupby(iter(vals), operator.itemgetter(0)):
> ... pr
What am I doing wrong here?
>>> import operator
>>> import itertools
>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15),
... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)]
>>> for k, g in itertools.groupby(iter(vals), operator.itemgetter(0)):
... print k, [i for i in g]
...
1 [(1, 11)]