我遇到过itertools.groupby
:
from itertools import groupby
from operator import itemgetter
source_data = ((1, 10), (1, 20), (1, 30), (1, 40), (2, 20))
d1 = {k: [x for x in v] for k, v in groupby(source_data, itemgetter(0))}
d2 = {k: v for k, v in groupby(source_data, itemgetter(0))}
d2 = {k: [x for x in v] for k, v in d2.items()}
print(d1)
print(d2)
作为输出我得到:
{1: [(1, 10), (1, 20), (1, 30), (1, 40)], 2: [(2, 20)]}
{1: [], 2: [(2, 20)]}
为什么这两个结果不同?
它是Python库中的一些错误,还是我错误地使用它?
我正在使用Python 3.5