如何从两个列表中找到匹配项? [英] How to find match items from two lists?
本文介绍了如何从两个列表中找到匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
Python:如何找到列表交集?
我在 .txt 中有两个数据列表
I have two lists of data in a .txt
data1 = "name1", "name2", "name3", "name4" etc.
data2 = "name3", "name6", "name10" etc.
我想找出哪些名字出现在两个列表中我该怎么做?
I want to find out which names appears in both list How would I do it?
推荐答案
使用 集合:
set(data1) & set(data2)
&
运算符的意思是给我这两个集合的交集";或者你可以使用 .intersection
方法:
The &
operator means "give me the intersection of these two sets"; alternatively you can use the .intersection
method:
set(data1).intersection(data2)
这篇关于如何从两个列表中找到匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文