简体   繁体   English

在python中查找两个字符串列表的交集

[英]Finding intersection of two lists of strings in python

I have gone through Find intersection of two lists? 我已经通过查找两个列表的交集? , Intersection of Two Lists Of Strings , Getting intersection of two lists in python . 两个字符串列表的 交集,在python中获取两个列表的交集 However, I could not solve this problem of finding intersection between two string lists using Python. 但是,我无法解决使用Python在两个字符串列表之间找到交集的问题。

I have two variables. 我有两个变量。

A = [['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]

B  = [['23@N0'], ['12@N1']]

How to find that '23@N0' is a part of both A and B? 如何找到'23 @ N0'是A和B的一部分?

I tried using intersect(a,b) as mentioned in http://www.saltycrane.com/blog/2008/01/how-to-find-intersection-and-union-of/ But, when I try to convert A into set, it throws an error: 我尝试使用http://www.saltycrane.com/blog/2008/01/how-to-find-intersection-and-union-of/中提到的交叉(a,b)但是,当我尝试转换A时进入集合,它会抛出一个错误:

File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'

To convert this into a set, I used the method in TypeError: unhashable type: 'list' when using built-in set function where the list can be converted using 要将其转换为集合,我使用TypeError中的方法:不可用类型:'list'使用内置set函数时可以使用

result = sorted(set(map(tuple, A)), reverse=True)

into a tuple and then the tuple can be converted into a set. 进入一个元组,然后元组可以转换成一个集合。 However, this returns a null set as the intersection. 但是,这会返回一个空集作为交集。

Can you help me find the intersection? 你能帮我找到路口吗?

You can use flatten function of compiler.ast module to flatten your sub-list and then apply set intersection like this 您可以使用compiler.ast模块的flatten函数来展平您的子列表,然后像这样应用set intersection

from compiler.ast import flatten

A=[['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]
B=[['23@N0'], ['12@N1']]

a = flatten(A)
b = flatten(B)
common_elements = list(set(a).intersection(set(b)))
common_elements
['23@N0']

Your datastructure is a bit strange, as it is a list of one-element lists of strings; 您的数据结构有点奇怪,因为它是一个单元素字符串列表的列表; you'd want to reduce it to a list of strings, then you can apply the previous solutions: 你想将它减少到一个字符串列表,然后你可以应用以前的解决方案:

Thus a list like: 因此列表如下:

B = [['23@N0'], ['12@N1']]

can be converted to iterator that iterates over '23@N0', '12@N1' 可以转换为遍历'23@N0', '12@N1'迭代器

with itertools.chain(*) , thus we have simple oneliner: 使用itertools.chain(*) ,因此我们有简单的oneliner:

>>> set(chain(*A)).intersection(chain(*B))
{'23@N0'}

The problem is that your lists contain sublists so they cannot be converted to sets. 问题是您的列表包含子列表,因此无法将它们转换为集合。 Try this: 尝试这个:

A=[['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]
B=[['23@N0'], ['12@N1']]

C = [item for sublist in A for item in sublist]
D = [item for sublist in B for item in sublist]

print set(C).intersection(set(D))

如果你必须把它装在幸运饼干上:

set(i[0] for i in A).intersection(set(i[0] for i in B))

You have two lists of lists with one item each. 您有两个列表列表,每个列表都有一个项目。 In order to convert that to a set you have to make it a list of strings: 为了将其转换为集合,您必须使其成为字符串列表:

set_a = set([i[0] for i in A])
set_b = set([i[0] for i in B])

Now you can get the intersection: 现在你可以得到交叉点:

set_a.intersection(set_b)
A=[['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]
A=[a[0] for a in A]
B=[['23@N0'], ['12@N1']]
B=[b[0] for b in B]
print set.intersection(set(A),set(B))

Output: set(['23@N0']) 输出: set(['23@N0'])

If each of your list has sublists of only 1 element you can try this. 如果您的每个列表都只有1元素的子列表,您可以尝试这个。

My preference is to use itertools.chain from the standard library: 我的偏好是使用标准库中的itertools.chain

from itertools import chain

A = [['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]

B = [['23@N0'], ['12@N1']]

set(chain(*A)) & set(chain(*B))

# {'23@N0'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM