Python groupby多次使用同一个键

Python groupby多次使用同一个键,python,iterator,group-by,Python,Iterator,Group By,我不认为我发现了一个错误,但它看起来并不正常 from itertools import groupby from operator import itemgetter c=[((u'http://www.example.com', u'second_value'), u'one'), ((u'http://www.example.com', u'second_value'), u'two'), ((u'http://www.hello.com', u'second_value'

我不认为我发现了一个错误,但它看起来并不正常

from itertools import groupby
from operator import itemgetter
c=[((u'http://www.example.com', u'second_value'), u'one'), 
   ((u'http://www.example.com', u'second_value'), u'two'), 
   ((u'http://www.hello.com', u'second_value'), u'one'), 
   ((u'http://www.example.com', u'second_value'), u'three'), 
   ((u'http://www.hello.com', u'second_value'), u'two')]
b= groupby(c, key=itemgetter(0))
for unique_keys, group in b:
    print unique_keys
收益率:

(u'http://www.example.com', u'second_value')
(u'http://www.hello.com', u'second_value')
(u'http://www.example.com', u'second_value')
(u'http://www.hello.com', u'second_value')

有什么解释吗?(我只希望有两把不同的钥匙)。我使用的是Python2.7.1,如果这有什么不同的话

需要对iterable进行排序(在同一个键函数上):

输出:


哦,好的。非常感谢,我现在不能对你的答案投赞成票,因为我没有超过15%的声誉。文档页面上没有真正的警告。除非考虑到其他语言/库中的大多数其他groupby函数都不需要它,否则人们不会这么做。@thg435--我会尽量不过度使用它,谢谢您的编辑。
from itertools import groupby
from operator import itemgetter
c=[((u'http://www.example.com', u'second_value'), u'one'), 
   ((u'http://www.example.com', u'second_value'), u'two'), 
   ((u'http://www.hello.com', u'second_value'), u'one'), 
   ((u'http://www.example.com', u'second_value'), u'three'), 
   ((u'http://www.hello.com', u'second_value'), u'two')]
b= groupby(sorted(c,key=itemgetter(0)), key=itemgetter(0))
for unique_keys, group in b:
    print unique_keys
(u'http://www.example.com', u'second_value')
(u'http://www.hello.com', u'second_value')