我有以下列表,其中包含与键/值相关的元组:
[[1, (62832.0, 1279365864.0, 20361.692513368984, 1729670.0, 7)], [2, (62832.0, 3011978242.0, 47937.01047237077, 60600190.0, 7)], [3, (62832.0, 62832.0, 1.0, 0.0, 7)], [9, (62832.0, 62832.0, 1.0, 0.0, 7)], [18, (62832.0, 18859400.0, 300.15597147950086, 109.0, 7)], [22, (62832.0, 1507339.6799999897, 23.989999999999835, 0.0, 7)], [44, (62832.0, 0.0, 0.0, 0.0, 7)], [1, u'18089'], [2, u'34414'], [3, u'1'], [4, u''], [5, u''], [6, u''], [7, u''], [8, u''], [9, u'1'], [10, u''], [11, u''], [16, u''], [18, u'1000.0'], [19, u''], [20, u''], [21, u''], [22, u'23.99'], [23, u''], [24, u''], [25, u''], [26, u''], [27, u''], [28, u''], [29, u''], [36, u''], [37, u''], [38, u''], [39, u''], [40, u''], [41, u''], [42, u''], [44, u'0'], [46, u''], [1, u'24245'], [2, u'64940'], [3, u'1'], [4, u''], [5, u''], [6, u''], [7, u''], [8, u''], [9, u'1'], [10, u''], [11, u''], [16, u''], [18, u'300.0'], [19, u''], [20, u''], [21, u''], [22, u'23.99'], [23, u''], [24, u''], [25, u''], [26, u''], [27, u''], [28, u''], [29, u''], [36, u''], [37, u''], [38, u''], [39, u''], [40, u''], [41, u''], [42, u''], [44, u'0'], [46, u''], [1, 1315.1691906367028], [2, 7784.612385983004], [3, 0.0], [9, 0.0], [18, 10.44030650891055], [22, 0.0], [44, 0.0], [1, 0.0], [2, 0.0], [3, 0.0], [9, 0.0], [18, 0.0], [22, 0.0], [44, 0.0]]
我想要做的是拉动钥匙并将它们组合在一起,如下所示:
[1, (62832.0, 1279365864.0, 20361.692513368984, 1729670.0, 7), u'18089', u'24245'], [2, etc], [3, etc]
任何想法如何进行这种加入/分组到列表
答案 0 :(得分:1)
您可以使用defaultdict执行此操作:
from collections import defaultdict
d = defaultdict(list)
for key, value in yourlist:
d[key].append(value)
result = [[key] + value for key, value in d.items()]