Python - 追加其他列表中的缺失元素
给定 2 个列表,将列表 1 中缺少的元素追加到列表 2。
Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 8, 10]
Output : [5, 6, 4, 1, 9, 8, 10]
Explanation : 5, 6, 4, 1 added to list 2, in order.
Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 10]
Output : [5, 6, 4, 8, 1, 9, 10]
Explanation : 5, 6, 4, 8, 1 added to list 2, in order.
方法#1:使用列表推导
在此,我们迭代列表 1 以检查列表 2 中缺失的元素,然后将这些元素添加到列表 2。
Python3
# Python3 code to demonstrate working of
# Append Missing elements from other List
# Using list comprehension
# initializing list
test_list1 = [5, 6, 4, 8, 9, 1]
test_list2 = [9, 8, 7]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# extracting elements from list 1 which are not in list 2
temp1 = [ele for ele in test_list1 if ele not in test_list2]
# constructing result
res = temp1 + test_list2
# printing result
print("The modified list 2 : " + str(res))
Python3
# Python3 code to demonstrate working of
# Append Missing elements from other List
# Using set() + "-" operator + extend()
# initializing list
test_list1 = [5, 6, 4, 8, 9, 1]
test_list2 = [9, 8, 7]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# finding missing words
rem_list = (set(test_list1) - set(test_list2))
# checking order
res = [ele for ele in test_list1 if ele in rem_list]
# joining result
res.extend(test_list2)
# printing result
print("The modified list 2 : " + str(res))
输出
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]
方法 #2:使用 set() + “-”运算符+ extend()
在此,我们使用 set() 和 –运算符检查列表 2 中缺少的列表 1 的元素,并使用 extend() 来连接两个列表以获得所需的结果。
Python3
# Python3 code to demonstrate working of
# Append Missing elements from other List
# Using set() + "-" operator + extend()
# initializing list
test_list1 = [5, 6, 4, 8, 9, 1]
test_list2 = [9, 8, 7]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# finding missing words
rem_list = (set(test_list1) - set(test_list2))
# checking order
res = [ele for ele in test_list1 if ele in rem_list]
# joining result
res.extend(test_list2)
# printing result
print("The modified list 2 : " + str(res))
输出
The original list 1 is : [5, 6, 4, 8, 9, 1]
The original list 2 is : [9, 8, 7]
The modified list 2 : [5, 6, 4, 1, 9, 8, 7]