Python 在一个列表中查找不在另一个列表中的元素 [英] Python find elements in one list that are not in the other

查看:80
本文介绍了Python 在一个列表中查找不在另一个列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个列表,以便创建一个包含在一个列表中但不在另一个列表中的特定元素的新列表.例如:

I need to compare two lists in order to create a new list of specific elements found in one list but not in the other. For example:

main_list=[]
list_1=["a", "b", "c", "d", "e"]
list_2=["a", "f", "c", "m"] 

我想遍历 list_1 并将 list_2 中所有在 list_1 中找不到的元素追加到 main_list.

I want to loop through list_1 and append to main_list all the elements from list_2 that are not found in list_1.

结果应该是:

main_list=["f", "m"]

我如何用 python 来做到这一点?

How can I do it with python?

推荐答案

TL;DR:
解决方案 (1)

import numpy as np
main_list = np.setdiff1d(list_2,list_1)
# yields the elements in `list_2` that are NOT in `list_1`

解决方案 (2) 你想要一个排序列表

def setdiff_sorted(array1,array2,assume_unique=False):
    ans = np.setdiff1d(array1,array2,assume_unique).tolist()
    if assume_unique:
        return sorted(ans)
    return ans
main_list = setdiff_sorted(list_2,list_1)



解释:
(1) 你可以使用 NumPy 的 setdiff1d (array1,array2,assume_unique=False).

EXPLANATIONS:
(1) You can use NumPy's setdiff1d (array1,array2,assume_unique=False).

assume_unique 询问用户数组是否已经是唯一的.
如果False,则首先确定唯一元素.
如果 True,函数将假定元素已经是唯一的,并且函数将跳过确定唯一元素.

assume_unique asks the user IF the arrays ARE ALREADY UNIQUE.
If False, then the unique elements are determined first.
If True, the function will assume that the elements are already unique AND function will skip determining the unique elements.

这会在array1 中产生array2没有的唯一值.assume_unique 默认为 False.

This yields the unique values in array1 that are not in array2. assume_unique is False by default.

如果您关心独特的元素(基于 Chinny84 的回复),然后简单地使用(其中 assume_unique=False => 默认值):

If you are concerned with the unique elements (based on the response of Chinny84), then simply use (where assume_unique=False => the default value):

import numpy as np
list_1 = ["a", "b", "c", "d", "e"]
list_2 = ["a", "f", "c", "m"] 
main_list = np.setdiff1d(list_2,list_1)
# yields the elements in `list_2` that are NOT in `list_1`


(2)对于那些想要对答案进行排序的人,我做了一个自定义函数:


(2) For those who want answers to be sorted, I've made a custom function:

import numpy as np
def setdiff_sorted(array1,array2,assume_unique=False):
    ans = np.setdiff1d(array1,array2,assume_unique).tolist()
    if assume_unique:
        return sorted(ans)
    return ans

要获得答案,请运行:

main_list = setdiff_sorted(list_2,list_1)

<小时>

边注:
(a) 解决方案 2(自定义函数 setdiff_sorted)返回一个 list(相对于 array 在解决方案 1) 中.

(b) 如果您不确定元素是否唯一,只需在解决方案 A 和 B 中使用 NumPy 的 setdiff1d 的默认设置.有什么复杂的例子?见注(c).

(c) 如果两个列表中的任何一个唯一,情况就会不同.
list_2 不是唯一的:list2 = ["a", "f", "c", "m", "m"].保持 list1 不变:list_1 = ["a", "b", "c", "d", "e"]
设置默认值assume_unique 产生 ["f", "m"] (在两种解决方案中).然而,如果你设置 assume_unique=True,两种解决方案都会给出 ["f", "m", "m"].为什么?这是因为用户假设元素是唯一的).因此,最好将 assume_unique 保持为默认值.请注意,两个答案都已排序.


SIDE NOTES:
(a) Solution 2 (custom function setdiff_sorted) returns a list (compared to an array in solution 1).

(b) If you aren't sure if the elements are unique, just use the default setting of NumPy's setdiff1d in both solutions A and B. What can be an example of a complication? See note (c).

(c) Things will be different if either of the two lists is not unique.
Say list_2 is not unique: list2 = ["a", "f", "c", "m", "m"]. Keep list1 as is: list_1 = ["a", "b", "c", "d", "e"]
Setting the default value of assume_unique yields ["f", "m"] (in both solutions). HOWEVER, if you set assume_unique=True, both solutions give ["f", "m", "m"]. Why? This is because the user ASSUMED that the elements are unique). Hence, IT IS BETTER TO KEEP assume_unique to its default value. Note that both answers are sorted.

这篇关于Python 在一个列表中查找不在另一个列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆