リスト内のインデックスの要素を削除するPythonプログラム
リスト内のインデックスの要素を削除する必要がある場合は、「enumerate」属性、「not in」演算子、単純な反復、および「append」メソッドが使用されます。
例
以下は同じのデモンストレーションです-
my_list = [91, 75, 15, 45, 69, 78, 23, 71, 36, 72]
print("The list is : " )
print(my_list)
print("The list after sorting is : " )
my_list.sort()
print(my_list)
index_list = [2, 4, 5, 7]
print("The index values stored in the list are :")
print(index_list)
my_result = []
for index, element in enumerate(my_list):
if index not in index_list:
my_result.append(element)
print("The resultant list is : ")
print(my_result)
print("The list after sorting is : " )
my_result.sort()
print(my_result) 出力
The list is : [91, 75, 15, 45, 69, 78, 23, 71, 36, 72] The list after sorting is : [15, 23, 36, 45, 69, 71, 72, 75, 78, 91] The index values stored in the list are : [2, 4, 5, 7] The resultant list is : [15, 23, 45, 72, 78, 91] The list after sorting is : [15, 23, 45, 72, 78, 91]
説明
-
リストが定義され、コンソールに表示されます。
-
並べ替えられ、コンソールに表示されます。
-
インデックス値はリストに保存されます。
-
コンソールにも表示されます。
-
空のリストが作成されます。
-
リストが繰り返され、「if」条件が設定されます。
-
これにより、インデックスがインデックス値リストに存在しないかどうかがチェックされます。
-
そうでない場合、要素は空のリストに追加されます。
-
これは、コンソールに出力として表示されます。
-
リストは再度ソートされ、コンソールに表示されます。
-
リストから重複要素を削除するPythonプログラム?
1つのリストには重複要素が含まれています。私たちのタスクは、重複なしの要素を含む別のリストを作成することです。 例 A::[2,3,4,3,4,6,78,90] Output::[2,3,4,6,78,90] アルゴリズム Step 1: create a list. Step 2: create a new list which is empty. Step 3: traverse every element in list. Step 4: if element is not present in the list return true. Step 5: append in the
-
リストのすべてのサブリストを出力するPythonプログラム。
リストを指定して、リストのすべてのサブリストを印刷します。 例- Input : list = [1, 2, 3] Output : [], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] アルゴリズム Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of th