我需要比较两个列表,以便创建一个在一个列表中找到而在另一个列表中没有的特定元素的新列表。例如:
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来做呢?
当前回答
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']
其他回答
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_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']
我将这些列表压缩在一起,逐个元素进行比较。
main_list = [b for a, b in zip(list1, list2) if a!= b]
如果要考虑出现的次数,则可能需要使用集合之类的方法。计数器:
list_1=["a", "b", "c", "d", "e"]
list_2=["a", "f", "c", "m"]
from collections import Counter
cnt1 = Counter(list_1)
cnt2 = Counter(list_2)
final = [key for key, counts in cnt2.items() if cnt1[key] != counts]
>>> final
['f', 'm']
如前所述,这也可以处理不同数量的事件作为“差异”:
list_1=["a", "b", "c", "d", "e", 'a']
cnt1 = Counter(list_1)
cnt2 = Counter(list_2)
final = [key for key, counts in cnt2.items() if cnt1[key] != counts]
>>> final
['a', 'f', 'm']
从ser1中删除ser2中的条目。
输入
1 = pd。系列([1、2、3、4、5]) ser2 = pd。系列([4、5、6、7、8])
解决方案
ser1[~ser1.isin(ser2)]