El *_*red 5 python arrays loops list
这个让我很头疼,而且我很难找到一个带有for循环的解决方案.
基本上,我的数据看起来像这样:
short_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ]
long_list = [ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13] ]
Run Code Online (Sandbox Code Playgroud)
我需要知道short_list中每行中的每个数字出现在long_list的每一行中的次数,并且当两个列表索引相同时不需要进行比较,因为它们来自相同的数据集.
示例:我需要知道long_list行[2,3,4,5,6],[6,7,8,9,10]和[9]中[1,2,3]中每个数字的出现次数. 10,11,12,13].然后继续short_list中的下一个数据行等.
for L1 in short_list:
for L2 in long_list:
if not set(L1).issubset(set(L2)):
for x in L1:
print("{} has {} occurrences in {}".format(x, L2.count(x), L2))
Run Code Online (Sandbox Code Playgroud)