[英]Comparing two lists of tuples and finding the frequency of the common elements
I have two lists of tuples: 我有两个元组列表:
myList1=[(1,2,3,4),(5,6,7),(8,9,10,11,12)]
myList2=[(1,2,7,6,2,1,3),(5,3,2,1,8,9,6),(11,12,1,2,5,6,6)]
I want to find the frequency of the elements in myList2
, which are common to myList1
and add them. 我想找到的元素的频率
myList2
,这是共同的myList1
和添加。
Something like this: 像这样:
5 5
3 3
2 2
2 2
2 2
3 3
0 0
2 2
2 2
Explanation: 5 # In myList2
, elements of myList1
occurred 5 times. 说明:5#在
myList2
,元素myList1
发生了5次。 ie 1 (2 times), 2 (2 times) and 3 (1 time) , hence 5. Same applies for other results. 即1(2次),2(2次)和3(1次),因此为5。其他结果也相同。 I tried looping over two lists and using
count
, but it didn't work. 我尝试遍历两个列表并使用
count
,但这没有用。
myList1=[(1,2,3,4),(5,6,7),(8,9,10,11,12)] myList1 = [(1,2,3,4),(5,6,7),(8,9,10,11,12)]
myList2=[(1,2,7,6,2,1,3),(5,3,2,1,8,9,6),(11,12,1,2,5,6,6)] myList2 = [(1,2,7,6,2,1,3),(5,3,2,1,8,9,6),(11,12,1,2,5,6,6) ]
count = 0
for el in zip(myList1,myList2):
for t in el[1]:
if t in el[0]:
count += 1
print(el)
print(count)
count = 0
Output: 输出:
((1, 2, 3, 4), (1, 2, 7, 6, 2, 1, 3)) (((1,2,3,4),(1,2,7,6,2,1,3))
5 5
((5, 6, 7), (5, 3, 2, 1, 8, 9, 6)) ((5、6、7),(5、3、2、1、8、9、6))
2 2
((8, 9, 10, 11, 12), (11, 12, 1, 2, 5, 6, 6)) ((8、9、10、11、12),(11、12、1、2、5、6、6))
2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.