我必须创建一个接受两个列表的函数。第一个是像 ["tant", "", "you"] 这样的单词列表,第二个列表有 ["hi", "something", "both", "sporte "]。我想要一本字典,其中包含第一个列表中的单词在第二个列表中出现的次数。
我的代码给出了相反的结果,即给出了第二个列表中的单词在第一个列表中出现的次数。 这是我的代码:
from collections import Counter
timesmumber def (x, y):
number = x
dicitio = Counter (number)
return dict (dicitio)
请您参考如下方法:
假设你有一个列表
first_list = ['tant', '', 'you']
和第二个列表
second_list = ['hi', 'something', 'both', 'sporte', 'tant']
您可以使用内置的count 方法创建字典
{nr : second_list.count(nr) for nr in first_list}
