简体   繁体   English

找到两个列表之间的共同元素?

[英]finding common elements between two lists?

I have two lists that are formatted as so:我有两个格式如下的列表:

sortedAlt=[['TIF35', 'TIF35'], ['PTP1', 'SSM4'], ['AMD1', 'PRP40'], ['END3', 'RAD26']]
sortedB=[['SDP1', 'SLT2'], ['ATG34', 'GCD7'], ['END3', 'RAD26'], ['TIF35', 'TIF35']]

and I need to find the shared elements between the two lists.我需要找到两个列表之间的共享元素。 I have tried a couple things:我尝试了几件事:

These two return only []:这两个只返回 []:

sharedEdges = []
temp = [sharedEdges.add((a, b)) for (a, b) in sortedB  
              if (a, b) in sortedAlt]

for (a,b) in sortedAlt:
    print((a,b))
    if (a,b) in sortedB:
        sharedEdges.add((a,b))
print(sharedEdges)

For the above code, I'm pretty sure the issue is within the if statement;对于上面的代码,我很确定问题出在 if 语句中; if I used the same list input for both, I still get an empty output.如果我对两者使用相同的列表输入,我仍然得到一个空的 output。

If I try to use the intersection function, I get the error: TypeError: unhashable type: 'list'如果我尝试使用交集 function,我会收到错误:TypeError: unhashable type: 'list'

sharedEdges = set(sortedAlt) & set(sortedB)
sharedEdges = list(set(sortedAlt)-set(sortedAlt))
print(sharedEdges)

Convert the inner lists to tuples, so they are hashable.将内部列表转换为元组,因此它们是可散列的。 Then you can use然后你可以使用

sortedAlt=[['TIF35', 'TIF35'], ['PTP1', 'SSM4'], ['AMD1', 'PRP40'], ['END3', 'RAD26']]
sortedB=[['SDP1', 'SLT2'], ['ATG34', 'GCD7'], ['END3', 'RAD26'], ['TIF35', 'TIF35']]

sharedEdges = set(tuple(i) for i in sortedAlt).intersection( (tuple(i) for i in sortedB))
print(sharedEdges)

Output: Output:

{('END3', 'RAD26'), ('TIF35', 'TIF35')}

This is faster for bigger lists then using a list comprehension with in and probably on par with OlvinRoghts commented map - solution:对于更大的列表,这比使用in的列表理解更快,并且可能与 OlvinRoghts 评论 map 相提并论 - 解决方案:

 # slightly modified from OlvinRoght comment on the question: s = list(map(list, set(map(tuple, sortedAlt)).intersection(map(tuple, sortedB)))) # [['TIF35', 'TIF35'], ['END3', 'RAD26']]

considering inner array element can be non sorted way, sorting them according to the character order then comparing考虑到内部数组元素可以是非排序方式,根据字符顺序对它们进行排序然后比较

A=[['TIF35', 'TIF35'], ['PTP1', 'SSM4'], ['AMD1', 'PRP40'], ['END3', 'RAD26']]
B=[['SDP1', 'SLT2'], ['ATG34', 'GCD7'], ['END3', 'RAD26'], ['TIF35', 'TIF35']]

tmp_b = [sorted(inner_list) for inner_list in B]
res = []
for inner_list in A:
    if sorted(inner_list) in tmp_b:
        res.append(inner_list)

print(res)
#OUTPUT  [['TIF35', 'TIF35'], ['END3', 'RAD26']]

considering the inner list order of element not matter then using set operation as suggested by @patrickartner, this will make time complexit small考虑到元素的内部列表顺序无关紧要,然后按照@patrickartner 的建议使用集合操作,这将使时间复杂度变小

res = list(set(tuple(i) for i in A).intersection(set(tuple(i) for i in B)))

below (convert the list to tuple and use set)下面(将列表转换为元组并使用集合)

sortedAlt=[['TIF35', 'TIF35'], ['PTP1', 'SSM4'], ['AMD1', 'PRP40'], ['END3', 'RAD26']]
sortedB=[['SDP1', 'SLT2'], ['ATG34', 'GCD7'], ['END3', 'RAD26'], ['TIF35', 'TIF35']]

sortedAltTuple = set(tuple(x) for x in sortedAlt)
sortedBTuple = set(tuple(x) for x in sortedB)
intersections = sortedAltTuple.intersection(sortedBTuple)
print(intersections)

output output

{('END3', 'RAD26'), ('TIF35', 'TIF35')}

The simpler way is with sets indeed but set accepts hashable objects only .更简单的方法确实是使用集合,但集合只接受可哈希对象 Lists are not hashable but tuples are.列表不可哈希,但元组可以。 So the simpler is simply to cast lists to tuples before constructing sets.所以更简单的是在构造集合之前将列表转换为元组。

shared_edges = set(map(tuple, sorted_alt)) & set(map(tuple, sorted_b))
shared_edges = list(map(list, shared_edges))  # then go back to lists if you want

By the way, please use the snake case ( shared_edges ) rather than the mixed case ( sharedEdges ).顺便说一句,请使用蛇形大小写 ( shared_edges ) 而不是混合大小写 ( sharedEdges )。 This is Python coding convention, see PEP8 .这是 Python 编码约定,请参阅PEP8

I have two lists that are formatted as so:我有两个格式如下的列表:

sortedAlt=[['TIF35', 'TIF35'], ['PTP1', 'SSM4'], ['AMD1', 'PRP40'], ['END3', 'RAD26']]
sortedB=[['SDP1', 'SLT2'], ['ATG34', 'GCD7'], ['END3', 'RAD26'], ['TIF35', 'TIF35']]

and I need to find the shared elements between the two lists.我需要找到两个列表之间的共享元素。 I have tried a couple things:我尝试了几件事:

These two return only []:这两个只返回 []:

sharedEdges = []
temp = [sharedEdges.add((a, b)) for (a, b) in sortedB  
              if (a, b) in sortedAlt]

for (a,b) in sortedAlt:
    print((a,b))
    if (a,b) in sortedB:
        sharedEdges.add((a,b))
print(sharedEdges)

For the above code, I'm pretty sure the issue is within the if statement;对于上面的代码,我很确定问题出在 if 语句中; if I used the same list input for both, I still get an empty output.如果我对两者使用相同的列表输入,我仍然会得到一个空输出。

If I try to use the intersection function, I get the error: TypeError: unhashable type: 'list'如果我尝试使用相交函数,我会收到错误:TypeError: unhashable type: 'list'

sharedEdges = set(sortedAlt) & set(sortedB)
sharedEdges = list(set(sortedAlt)-set(sortedAlt))
print(sharedEdges)

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

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