Re: A problem with itertools.groupby
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's a shorter and less readable illustration: >>> list(groupby("aabbb")) [('a', ), ('b', )] >>> list(groupby("aabbb"))[0] ('a', ) >>> list(list(groupby("aabbb"))[0][1]) [] -- To email me, substitute nowhere->runbox, invalid->com. -- https://mail.python.org/mailman/listinfo/python-list
Re: A problem with itertools.groupby
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 why, this is > the first read of an iterator, it should provide its > data. > https://docs.python.org/3/library/itertools.html#itertools.groupby Check the explanatory third paragraph :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: A problem with itertools.groupby
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 iterator with an extra termination condition. So in order to start the next group-iterator the previous group-iterator is exhausted, because the original iterator has to be ready to produce values for the next group-iterator. -- Antoon Pardon. -- https://mail.python.org/mailman/listinfo/python-list
A problem with itertools.groupby
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', 'n'] n [] b ['b'] b [] It works as expected. itertools._grouper objects are probably iterators so they provide their datas only once. OK 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. This one works: ["".join(grp) for k, grp in groupby("aahfffddnnb")] ['aa', 'h', 'fff', 'dd', '', 'nn', 'b'] regards -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with itertools.groupby.
[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 g] ... What I want is tuples starting with identical numbers to be grouped. I cannot figure out why this is not working. If anyone has any insights, I would appreciate it. - Alex Ross Sort the list before using it. vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)] def first(pair): return pair[0] for k, g in itertools.groupby(sorted(vals, key=first), first): print k, [i for i in g] groupby depends on the source stream having the clustering you need. Otherwise it could not work on the fly for arbitrarily large sources. Often you can arrange for your data source to be clustered; when you cannot, the groupby arg is a great sort key. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with itertools.groupby.
[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 g] ... 1 [(1, 11)] 2 [(2, 12)] 3 [(3, 13)] 4 [(4, 14)] 5 [(5, 15)] 1 [(1, 16)] 2 [(2, 17)] 3 [(3, 18)] 4 [(4, 19)] 5 [(5, 20)] What I want is tuples starting with identical numbers to be grouped. I cannot figure out why this is not working. If anyone has any insights, I would appreciate it. 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] /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with itertools.groupby.
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 contents in an iterator into a list object, list(g) is a bit more convenient than [i for i in g]. /F -- http://mail.python.org/mailman/listinfo/python-list