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什麼錯?