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

查看:2247
本文介绍了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) 您要排序的列表

SOLUTION (2) You want a sorted list

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 (array1array2assume_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中唯一的值,而中的不是. 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)返回一个 列表 (与解决方案中的 数组 相比) 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天全站免登陆