Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39966

groupby behaviour

Date 2013-02-26 16:27 +0000
Subject groupby behaviour
From andrea crotti <andrea.crotti.0@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2550.1361896083.2939.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

groupby behaviour andrea crotti <andrea.crotti.0@gmail.com> - 2013-02-26 16:27 +0000

csiph-web