Re: A problem with itertools.groupby

2021-12-17 Thread Peter Pearson
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.

Re: A problem with itertools.groupby

2021-12-17 Thread Chris Angelico
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

Re: A problem with itertools.groupby

2021-12-17 Thread Antoon Pardon
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

A problem with itertools.groupby

2021-12-17 Thread ast
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

Re: Problem with itertools.groupby.

2006-05-25 Thread Scott David Daniels
[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)): ... print k, [i for i in

Re: Problem with itertools.groupby.

2006-05-25 Thread Fredrik Lundh
[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)): ... print k, [i for i in

Re: Problem with itertools.groupby.

2006-05-25 Thread Fredrik Lundh
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 the