Python程序在两个列表中查找缺失值和附加值?
在集合论中,集合A的补码是指不在A中的元素。A相对于集合B的相对补码,也称为集合A与集合B的差。我们仅在此应用此原理。Python具有差异功能。
算法
Step 1 : first we create two user input list. A & B Step 2 : Insert A and B to a set. Step 3 : for finding the missing values of first list we apply difference function, difference of B from A. Step 4 : for finding the Additional values of first list we apply difference function, difference of A from B. Step 5 : Same procedure apply for Second list also.
范例程式码
#To find the missing and additional elements
A=list()
B=list()
n1=int(input("Enter the size of the First List ::"))
n2=int(input("Enter the size of the second List ::"))
print("Enter the Element of first List ::")
for i in range(int(n1)):
k=int(input(""))
A.append(k)
print("Enter the Element of second List ::")
for j in range(int(n2)):
k1=int(input(""))
B.append(k1)
# prints the missing and additional elements in first list
print("Missing values in first list:", (set(B).difference(A)))
print("Additional values in first list:", (set(A).difference(B)))
# prints the missing and additional elements in second list
print("Missing values in second list:", (set(A).difference(B)))
print("Additional values in second list:", (set(B).difference(A)))输出结果
Enter the size of the First List :: 6
Enter the size of the second List :: 5
Enter the Element of first List ::
1
2
3
4
5
6
Enter the Element of second List ::
4
5
6
7
8
Missing values in first list: {7, 8}
Additional values in first list: {1, 2, 3}
Missing values in second list: {1, 2, 3}
Additional values in second list: {7, 8}