Python 比较两个列表并打印常用元素的次数
我想比较items1和items2,并打印items1中出现的items2中最常见的数字 项目1中的数字是固定的,而项目2中的数字是更新的。我还要计算每个数字出现的次数。这就是我到目前为止的想法:Python 比较两个列表并打印常用元素的次数,python,python-2.7,list-comparison,Python,Python 2.7,List Comparison,我想比较items1和items2,并打印items1中出现的items2中最常见的数字 项目1中的数字是固定的,而项目2中的数字是更新的。我还要计算每个数字出现的次数。这就是我到目前为止的想法: import collections items1 = [12, 23, 34, 45, 56, 67, 78, 89, 13, 24, 35, 46, 57, 68, 79, 014] items2 = [528, 98, 925, 902, 67, 78, 89, 13, 24, 35, 46,
import collections
items1 = [12, 23, 34, 45, 56, 67, 78, 89, 13, 24, 35, 46, 57, 68, 79, 014]
items2 = [528, 98, 925, 902, 67, 78, 89, 13, 24, 35, 46, 57]
results = collections.Counter()
for _ in items:
number = (draws)
results.update([tuple(number)])
print results.most_common(3)
请尝试以下代码:
items1 = [12, 23, 34, 45, 56, 67, 78, 89, 13, 24, 35, 46, 57, 68, 79, 014]
items2 = [528, 98, 925, 902, 67, 78, 89, 13, 24, 35, 46, 57]
itemcount = {item: items2.count(item) for item in items1}
print [[number, itemcount[number]] for number in sorted(itemcount.keys(), key=lambda x:itemcount[x])[::-1][:5]]
#Prints [common value: occurences], as such: [[89, 1], [46, 1], [35, 1], [24, 1], [13, 1]]
那么你的问题是什么?顺便说一句,你不需要随机库。图纸和项目在哪里?@Rinzler yep,OP似乎缺少一些代码随机库未使用,图纸和项目不存在等等。