[issue14828] itertools.groupby not working as expected

2012-05-16 Thread Jiba

New submission from Jiba jibal...@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 rep...@bugs.python.org
http://bugs.python.org/issue14828
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14828] itertools.groupby not working as expected

2012-05-16 Thread Jiba

Jiba jibal...@free.fr added the comment:

Ok, I understand.

However, in my initial problem, the sequence passed to groupby was a set, e.g. 
(modifying my previous example) :

   groupby(set([p1, p2, p3]), lambda p: p.key)

If I understand well how groupby() works, the result of a groupby performed on 
a set is unpredictable, since it depends of the order of the items when 
iterating over the set. Perhaps the behavior of groupby() should be modified 
for unsorted sequences, possibly not taking the order into account, or raising 
an error ?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue14828
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com