我已经查阅了三篇文章,链接如下: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转换为set时,它会抛出一个错误:
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)
将其转换为元组,然后可以将元组转换为集合。但是这会返回一个空集作为交集。
您能帮助我找到交集吗?