python列表如何合并并把values相加
a=[['A','B',2.0],['B','C',2.0],['C','D',71.99],['C','B',267.7],['A','B',26.95],['D','...
a=[['A', 'B', 2.0],
['B', 'C', 2.0],
['C', 'D', 71.99],
['C', 'B', 267.7],
['A', 'B', 26.95],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0],
['B', 'C', 3.9331]]
如何得到
b=[['A', 'B', 28.95],
['B', 'C', 5.9331],
['C', 'D', 71.99],
['C', 'B', 267.7],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0]]
谢谢 ['C', 'B', 267.7],
['A', 'B', 26.95],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0],
['B', 'C', 3.9331]]
如何得到
b=[['A', 'B', 28.95],
['B', 'C', 5.9331],
['C', 'D', 71.99],
['C', 'B', 267.7],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0]]
谢谢 展开
['B', 'C', 2.0],
['C', 'D', 71.99],
['C', 'B', 267.7],
['A', 'B', 26.95],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0],
['B', 'C', 3.9331]]
如何得到
b=[['A', 'B', 28.95],
['B', 'C', 5.9331],
['C', 'D', 71.99],
['C', 'B', 267.7],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0]]
谢谢 ['C', 'B', 267.7],
['A', 'B', 26.95],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0],
['B', 'C', 3.9331]]
如何得到
b=[['A', 'B', 28.95],
['B', 'C', 5.9331],
['C', 'D', 71.99],
['C', 'B', 267.7],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0]]
谢谢 展开
1个回答
展开全部
a = [
['A', 'B', 2.0],
['B', 'C', 2.0],
['C', 'D', 71.99],
['C', 'B', 267.7],
['A', 'B', 26.95],
['D', 'C', 2.6328],
['B', 'D', 23.262],
['D', 'A', 10.0],
['B', 'C', 3.9331]
]
res = {}
for i in a:
if res.has_key(tuple(i[:2])):
res[tuple(i[:2])]=res[tuple(i[:2])]+i[2]
else:
res[tuple(i[:2])] = i[2]
result=[]
for k,v in res.items():
result.append([k[0],k[1],v])
print result
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询