[New-bugs-announce] [issue14828] itertools.groupby not working as expected

Jiba report at bugs.python.org
Wed May 16 11:51:34 CEST 2012


New submission from Jiba <jibalamy at free.fr>:

In some situation, itertools.groupby fails to group the objects, and produces several groups with the same key. For example, the following code :


from itertools import *

class P(object):
  def __init__(self, key):
    self.key = key

p1 = P(1)
p2 = P(2)
p3 = P(1)

for key, ps in groupby([p1, p2, p3], lambda p: p.key):
  print "group", key
  for p in ps:
    print "  - object", p


Produces the following result :

group 1
  - object <__main__.P object at 0xb73d6acc>
group 2
  - object <__main__.P object at 0xb73d6aec>
group 1
  - object <__main__.P object at 0xb73d6b0c>


While I would expect to have only a single group 1, e.g. something like :

group 1
  - object <__main__.P object at 0xb73d6acc>
  - object <__main__.P object at 0xb73d6b0c>
group 2
  - object <__main__.P object at 0xb73d6aec>


It seems that this bug also affects Python 3 (tested on Python 3.1.2)

----------
components: Library (Lib)
messages: 160822
nosy: Jiba
priority: normal
severity: normal
status: open
title: itertools.groupby not working as expected
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14828>
_______________________________________


More information about the New-bugs-announce mailing list