作者ZolaEmile (Zola)
看板Python
标题Re: [问题] 询问list如行相加
时间Thu Feb 4 00:23:53 2016
s = dict()
for a, b in zip(link_a + link_b + link_c, bookmark_a + bookmark_c + bookmark
_c): s[a] = s.get(a, 0) + b
※ 引述《busystudent (busystudent)》之铭言:
: hi 我想询问list若有重复的标签该如何相加
: 我有三组list,内容为个人所收藏的标签与其收藏次数,如下所示:
: link_a = ['a','b','c']
: bookmark_a = ['1','2','3']
: link_b = ['b','c']
: bookmark_c = ['4','5']
: link_c = ['a']
: bookmark_c = ['6']
: 我想做些计算,得到如下面的结果
: answer_link_all = ['a','b','c']
: answer_bookmark_all = ['7','6','8']
: 其实我一开始是打算 link_a+link_b = ['a','b','c','b','c']後来发现,名称会
: 重复,像是重复出现'b'和'c'之类的,所以打算写一个if判断式,可是考虑到又
: 有bookmark要去计算,就感到怪怪的,请大家给我提示,谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.109.32.9
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1454516635.A.842.html