2つのリストで欠落している値と追加の値を見つけるPythonプログラム?
集合論では、集合Aの補集合はAにない要素を指します。集合Bに対するAの相対的な補集合は、集合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}
-
リスト内の最大要素と最小要素の位置を見つけるPythonプログラム?
Pythonでは、最大要素、最小要素、およびそれらの位置も非常に簡単に見つけることができます。 Pythonはさまざまな組み込み関数を提供します。 min()は配列の最小値を見つけるために使用され、max()は配列の最大値を見つけるために使用されます。 index()は、要素のインデックスを見つけるために使用されます。 アルゴリズム maxminposition(A, n) /* A is a user input list and n is the size of the list.*/ Step 1: use inbuilt function for finding the positi
-
2つのリストの違いをリストするPythonプログラム。
この問題では、2つのリストが与えられます。私たちのタスクは、2つのリストの違いを表示することです。 Pythonはset()メソッドを提供します。ここではこの方法を使用します。セットは、重複する要素がない順序付けられていないコレクションです。セットオブジェクトは、和集合、共通部分、差、対称差などの数学演算もサポートしています。 例 Input::A = [10, 15, 20, 25, 30, 35, 40] B = [25, 40, 35] Output: [10, 20, 30, 15] 説明 difference list = A - B アルゴリズム Step 1: Inp