Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39966 > unrolled thread
| Started by | andrea crotti <andrea.crotti.0@gmail.com> |
|---|---|
| First post | 2013-02-26 16:27 +0000 |
| Last post | 2013-02-26 16:27 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
groupby behaviour andrea crotti <andrea.crotti.0@gmail.com> - 2013-02-26 16:27 +0000
| From | andrea crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2013-02-26 16:27 +0000 |
| Subject | groupby behaviour |
| Message-ID | <mailman.2550.1361896083.2939.python-list@python.org> |
So I was trying to use groupby (which I used in the past), but I
noticed a very strange thing if using list on
the result:
In [109]: s = sorted(s, key=keyfunc)
In [110]: s
Out[110]: [(1, 2), (1, 3), (1, 5), (2, 3)]
In [117]: m2 = list(groupby(s, keyfunc))
In [118]: for name, res in m2:
.....: for r in res:
.....: print name, r
.....:
2 (2, 3)
In [119]: m2 = groupby(s, keyfunc)
In [120]: for name, res in m2:
for r in res:
print name, r
.....:
1 (1, 2)
1 (1, 3)
1 (1, 5)
2 (2, 3)
How is that possible?
Generating the list first should be the same as iterating after, how
can it behave differently?
Back to top | Article view | comp.lang.python
csiph-web