我在下面列出了以下列表,并希望总结具有相同资产编号的资产值,以便我可以获得类似于下面的内容
Asset number Total
11230202 10500.5
11230101 79
11230213 0
11230228 0
资产编号列表:
[U' 11230202',你' 11230101',你' 11230202',你' 11230213',你' 11230202',你' 11230232',你' 11230202',你' 11230202',你' 11230202 ',你' 11230101',你' 11230202',你' 11230202',你' 11230228',你' 11230202',你' 11230202',你' 11230202',你' 11230202',你' 11230101 ']
资产价值表:
[10000.0,10,0,0,0,0,5.0,5,0,0,0,0,0,0,0,0,0,09]
答案 0 :(得分:0)
您可以使用collections.defaultdict
:
from collections import defaultdict
s = map(int, [u' 11230202 ', u' 11230101 ', u' 11230202 ', u' 11230213 ', u' 11230202 ', u' 11230232 ', u' 11230202 ', u' 11230202 ', u' 11230202 ', u' 11230101 ', u' 11230202 ', u' 11230202 ', u' 11230228 ', u' 11230202 ', u' 11230202 ', u' 11230202 ', u' 11230202 ', u' 11230101 '])
s1 = [10000.0, 10, 0, 0, 0, 0, 500.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69]
d = defaultdict(list)
for a, b in zip(s, s1):
d[a].append(b)
final_d = {a:sum(b) for a, b in d.items()}
for a, b in sorted(final_d.items(), key=lambda x:x[-1], reverse=True):
print(a, b)
输出:
(11230202, 10500.5)
(11230101, 79)
(11230232, 0)
(11230228, 0)
(11230213, 0)