一意キーへのPythonプログラムは、タプルリストの値にカウントされます
タプルのリスト内の値の一意キーの数を取得する必要がある場合は、それを繰り返して、それぞれの数を決定できます。
以下は同じのデモンストレーションです-
例
import collections
my_result = collections.defaultdict(int)
my_list = [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]]
print("The list of list is :")
print(my_list)
for elem in my_list:
my_result[elem[0]] += 1
print("The result is : ")
print(my_result) 出力
The list of list is :
[[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]]
The result is :
defaultdict(<class 'int'>, {('Hello', 'Hey'): 2, ('Jane', 'Will'): 1, ('William', 'John'): 1, ('z', 'q'): 1}) 説明
-
必要なパッケージがインポートされます。
-
文字列と文字を含むタプルのリストのリストが定義されています。
-
リストがコンソールに表示されます。
-
リストが繰り返され、最初の要素が1ずつ増加します。
-
この結果はコンソールに表示されます。
-
リストから一意の値を出力するPythonプログラム
リストが与えられた場合、私たちのタスクはすべての一意の番号を印刷することです。 例 Input:A = [1, 2, 3, 4, 2, 1, 9] Unique list is [1, 2, 3, 4, 9] アルゴリズム Step 1: Create user input list. Step 2: Create an empty list. Step 3: Traverse all elements in the list. Step 4: Check the unique element is present or not. Step 5: Append unique element o
-
べき乗剰余のためのPythonプログラム
x、y、zの3つの数値が与えられた場合、私たちのタスクは(x ^ y)%zを計算することです 例 Input: x = 2, y = 3, p = 3 Output: 2 説明 :2 ^ 3%3 =8%3=2。 アルゴリズム Step 1: Input three numbers. Step 2: then we use pow() to calculating power and % for modular. Step 3: display result. サンプルコード x = int(input(Enter First Value ::>)) y = int(inpu