【发布时间】:2016-02-22 21:56:16
【问题描述】:
假设我有一本字典:
lst = {'adore': 10, 'hate': 10, 'hello': 10, 'pigeon': 1, 'would': 5, 'elections': 5}
我有一个列表:
mylist = [['a new', 'party'], ['to', 'lol'], ['compete'], ['in', 'adore', 'the 2013'], ['federal', 'elections'], ['The Free', 'Voters'], ['leadership', 'declined to'], ['join forces', 'according to', 'a leaked'], ['email from', 'Bernd Lucke'], ['Advocating', 'adore'] ]
我希望能够在列表中搜索字典中的键。如果列表中的单词是键,则获取该键的值并将其添加到计数器。最后,得到所有值的总和。
有没有办法做到这一点?
【问题讨论】:
-
你的意思是
mylist = ['a', 'new', 'party', 'to', 'compete', 'in', 'the', '2013', 'federal', 'elections.',...]?还是mylist = [['a new', 'party'], ['to', 'lol'],...]? -
抱歉给大家带来了困惑,为了更有意义,我做了一些修改
-
啊,感谢编辑:D
-
现在,请检查我的答案是否正确。
标签: python list loops search dictionary