我有一个元组列表:
Listoftuples=[
(0.021892733407683305, 0.14887058717224647, 4.573173081530965, 0.04619366749021177, u'0102'),
(0.08416364174734663, 0.8500527816482009, 23.649983331004403, 0.0, u'0103'),
(0.02181070623592521, 0.15049387302788395, 1.2098398749067714, 1.6037412295275804, u'0102')
]
我想要每组(group =元组中的最后一个值,例如u'0102'):
尝试:
import itertools
Listoftuples=[
(0.021892733407683305, 0.14887058717224647, 4.573173081530965, 0.04619366749021177, u'0102'),
(0.08416364174734663, 0.8500527816482009, 23.649983331004403, 0.0, u'0103'),
(0.02181070623592521, 0.15049387302788395, 1.2098398749067714, 1.6037412295275804, u'0102')
]
keyfunc=lambda t: (t[4])
Listoftuples.sort(key=keyfunc)
for key,rows in itertools.groupby(Listoftuples, keyfunc):
sumOfFirstValue = sum(r[0] for r in rows)
sumOfSecondDividedBySumOfFirst= sum(r[1] for r in rows)/sumOfFirstValue
print key,sumOfFirstValue,sumOfSecondDividedBySumOfFirst
结果:
0102 0.0437034396436 0.0
0103 0.0841636417473 0.0
最后的零值。我该如何解决?
答案 0 :(得分:5)
一个常见的错误是认为从rows
返回的groupby
是一个具体列表。实际上它是一个迭代器,在计算sumOfFirstValue
时已经筋疲力尽了。解决方法是:
...
for key,rows in itertools.groupby(Listoftuples, keyfunc):
rows = list(rows)
...
答案 1 :(得分:1)
不使用itertools
的简单解决方案:
groups = set(item[4] for item in Listoftuples)
for g in groups:
sum_first_val = sum([item[0] for item in Listoftuples if item[4] == g])
sum_second_val = sum([item[1] for item in Listoftuples if item[4] == g])
print g, sum_first_val, sum_second_val / sum_first_val