2014-01-22 85 views
1

我試圖使用填充功能GROUPBY和itemgetter以重新安排元組的排序列表分組itertools.groupby返回空列表項,與operator.itemgetter

from itertools import groupby 
from operator import itemgetter 

#initialize a list of tuples 
indexed_qualityresults = [(u'moses-R4', 2.0), (u'moses-R4', 3.0), (u'lucy-R4', 3.0), (u'trados-R4', 2.0)] 

#group tuples, using as a key the first element of each tuple 
groupped_qualityresults = list(groupby(indexed_qualityresults, itemgetter(0))) 

#print the key and the respective grouped tuples for each group 
print "groupped_qualityresults =", [(a,list(b)) for a,b in groupped_qualityresults] 

輸出

groupped_qualityresults = [(u'moses-R4', []), (u'lucy-R4', []), (u'trados-R4', [(u'trados-R4', 2.0)])] 

正如你所看到的,那麼返回的tmy原始元組列表的兩個第一鍵的列表是空的,儘管它們不應該是。

預期輸出:

groupped_qualityresults = [(u'moses-R4', [(u'moses-R4', 2.0), (u'moses-R4', 3.0)]), (u'lucy-R4', [(u'lucy-R4', 3.0)]), (u'trados-R4', [(u'trados-R4', 2.0)])] 

有人可以indentify什麼錯?

回答

5

不要在groupby迭代器調用list()

#group tuples, using as a key the first element of each tuple 
groupped_qualityresults = groupby(indexed_qualityresults, itemgetter(0)) 

#print the key and the respective grouped tuples for each group 
print "groupped_qualityresults =", [(a,list(b)) for a,b in groupped_qualityresults] 

itertools.groupby() documentation

返回組本身是一個與groupby()共享迭代底層的迭代器。由於源是共享的,因此當groupby()對象進階時,先前的組不再可見。

將輸出從groupby()變成列表前進groupby()對象。