我需要比较两个列表,以便创建一个在一个列表中找到而在另一个列表中没有的特定元素的新列表。例如:
main_list=[]
list_1=["a", "b", "c", "d", "e"]
list_2=["a", "f", "c", "m"]
我想循环遍历list_1,并将list_2中没有在list_1中找到的所有元素附加到main_list。
结果应该是:
main_list=["f", "m"]
我怎么用python来做呢?
我需要比较两个列表,以便创建一个在一个列表中找到而在另一个列表中没有的特定元素的新列表。例如:
main_list=[]
list_1=["a", "b", "c", "d", "e"]
list_2=["a", "f", "c", "m"]
我想循环遍历list_1,并将list_2中没有在list_1中找到的所有元素附加到main_list。
结果应该是:
main_list=["f", "m"]
我怎么用python来做呢?
当前回答
我用了两种方法,我发现其中一种方法比另一种更有用。以下是我的回答:
我的输入数据:
crkmod_mpp = ['M13','M18','M19','M24']
testmod_mpp = ['M13','M14','M15','M16','M17','M18','M19','M20','M21','M22','M23','M24']
Method1: np。我喜欢这种方法,因为它保留了位置
test= list(np.setdiff1d(testmod_mpp,crkmod_mpp))
print(test)
['M15', 'M16', 'M22', 'M23', 'M20', 'M14', 'M17', 'M21']
Method2:虽然答案和Method1一样,但是打乱了顺序
test = list(set(testmod_mpp).difference(set(crkmod_mpp)))
print(test)
['POA23', 'POA15', 'POA17', 'POA16', 'POA22', 'POA18', 'POA24', 'POA21']
Method1 np。Setdiff1d完全符合我的要求。 这是信息的答案。
其他回答
我将这些列表压缩在一起,逐个元素进行比较。
main_list = [b for a, b in zip(list1, list2) if a!= b]
当你有本地方法可用时,不确定为什么上面的解释如此复杂:
main_list = list(set(list_2)-set(list_1))
TL;博士: 解决方案(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)。
assume_unique询问用户数组是否已经是唯一的。如果为False,则首先确定唯一元素。 如果为True,函数将假设元素已经是唯一的,并且函数将跳过确定唯一元素。
这将产生array1中不在array2中的唯一值。assume_unique默认为False。
如果你关心的是唯一元素(基于Chinny84的响应),那么只需使用(其中assume_unique=False =>为默认值):
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) 对于那些想要对答案进行排序的人,我已经做了一个自定义函数:
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)
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.
pythonnumpy
你可以使用集合:
main_list = list(set(list_2) - set(list_1))
输出:
>>> list_1=["a", "b", "c", "d", "e"]
>>> list_2=["a", "f", "c", "m"]
>>> set(list_2) - set(list_1)
set(['m', 'f'])
>>> list(set(list_2) - set(list_1))
['m', 'f']
根据@JonClements的评论,这里是一个更整洁的版本:
>>> list_1=["a", "b", "c", "d", "e"]
>>> list_2=["a", "f", "c", "m"]
>>> list(set(list_2).difference(list_1))
['m', 'f']
main_list=[]
list_1=["a", "b", "c", "d", "e"]
list_2=["a", "f", "c", "m"]
for i in list_2:
if i not in list_1:
main_list.append(i)
print(main_list)
输出:
['f', 'm']