在 python 中查找两个字符串列表的交集
Finding intersection of two lists of strings in python
我经历了Find intersection of two lists?, Intersection of Two Lists Of Strings, Getting intersection of two lists in python。但是,我无法解决使用 Python.
查找两个字符串列表之间的交集的问题
我有两个变量。
A = [['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]
B = [['23@N0'], ['12@N1']]
如何找到 '23@N0' 是 A 和 B 的一部分?
我尝试使用 http://www.saltycrane.com/blog/2008/01/how-to-find-intersection-and-union-of/ 中提到的 intersect(a,b)
但是,当我尝试将 A 转换为集合时,它会抛出一个错误:
File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'
为了将其转换为集合,我使用了 TypeError: unhashable type: 'list' when using built-in set function 中的方法,其中列表可以使用
进行转换
result = sorted(set(map(tuple, A)), reverse=True)
成一个元组,然后元组可以转换成一个集合。然而,这个returns一个空集作为交集。
你能帮我找到路口吗?
问题是您的列表包含子列表,因此无法将它们转换为集合。试试这个:
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_a = set([i[0] for i in A])
set_b = set([i[0] for i in B])
现在可以得到交集了:
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))
输出:set(['23@N0'])
如果您的每个列表都只有 1
个元素的子列表,您可以试试这个。
你的数据结构有点奇怪,因为它是一个由单元素字符串列表组成的列表;你想将它缩减为一个字符串列表,那么你可以应用以前的解决方案:
因此列表如下:
B = [['23@N0'], ['12@N1']]
可以转换为遍历'23@N0', '12@N1'
的迭代器
和itertools.chain(*)
,因此我们有简单的一行:
>>> set(chain(*A)).intersection(chain(*B))
{'23@N0'}
您可以使用 compiler.ast
模块的 flatten
函数来展平您的子列表,然后像这样应用集合交集
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']
如果你必须把它放在幸运饼干上:
set(i[0] for i in A).intersection(set(i[0] for i in B))
我的偏好是使用标准库中的 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'}
我经历了Find intersection of two lists?, Intersection of Two Lists Of Strings, Getting intersection of two lists in python。但是,我无法解决使用 Python.
查找两个字符串列表之间的交集的问题我有两个变量。
A = [['11@N3'], ['23@N0'], ['62@N0'], ['99@N0'], ['47@N7']]
B = [['23@N0'], ['12@N1']]
如何找到 '23@N0' 是 A 和 B 的一部分?
我尝试使用 http://www.saltycrane.com/blog/2008/01/how-to-find-intersection-and-union-of/ 中提到的 intersect(a,b) 但是,当我尝试将 A 转换为集合时,它会抛出一个错误:
File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'
为了将其转换为集合,我使用了 TypeError: unhashable type: 'list' when using built-in set function 中的方法,其中列表可以使用
进行转换result = sorted(set(map(tuple, A)), reverse=True)
成一个元组,然后元组可以转换成一个集合。然而,这个returns一个空集作为交集。
你能帮我找到路口吗?
问题是您的列表包含子列表,因此无法将它们转换为集合。试试这个:
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_a = set([i[0] for i in A])
set_b = set([i[0] for i in B])
现在可以得到交集了:
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))
输出:set(['23@N0'])
如果您的每个列表都只有 1
个元素的子列表,您可以试试这个。
你的数据结构有点奇怪,因为它是一个由单元素字符串列表组成的列表;你想将它缩减为一个字符串列表,那么你可以应用以前的解决方案:
因此列表如下:
B = [['23@N0'], ['12@N1']]
可以转换为遍历'23@N0', '12@N1'
和itertools.chain(*)
,因此我们有简单的一行:
>>> set(chain(*A)).intersection(chain(*B))
{'23@N0'}
您可以使用 compiler.ast
模块的 flatten
函数来展平您的子列表,然后像这样应用集合交集
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']
如果你必须把它放在幸运饼干上:
set(i[0] for i in A).intersection(set(i[0] for i in B))
我的偏好是使用标准库中的 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'}