python列表如何转字典? 取相同key值把values相加
比如说我有以下列表:['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','50...
比如说我有以下列表:
['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']
好像能通过Python的join,把该列表转化为字典??字典的key值为【'apple','ios'】和【'pear','android'】,把对应的values【100+500】【10+50】,【200+600】【20+60】相加,要实现的结果如下:
['apple','ios','600','60'],['pear','android','800','80']
具体用python怎么实现,正在学习中~请大侠指教下详细代码怎么写,万分感谢!!! 展开
['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']
好像能通过Python的join,把该列表转化为字典??字典的key值为【'apple','ios'】和【'pear','android'】,把对应的values【100+500】【10+50】,【200+600】【20+60】相加,要实现的结果如下:
['apple','ios','600','60'],['pear','android','800','80']
具体用python怎么实现,正在学习中~请大侠指教下详细代码怎么写,万分感谢!!! 展开
1个回答
展开全部
mobile=[['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']]
mobiledict={}
for elem in mobile:
key=(elem[0],elem[1])
if key in mobiledict:
mobiledict[key][0]+=int(elem[2])
mobiledict[key][1]+=int(elem[3])
else:
mobiledict[key]=[int(elem[2]),int(elem[3])]
print(mobiledict)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询