Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonのN要素の増分タプル


'N'要素のインクリメンタルタプルを作成する必要がある場合は、ジェネレータ式と'tuple'メソッドを使用できます。

以下は同じのデモンストレーションです-

N = 3
print("The value of 'N' has been initialized")
print("The number of times it has to be repeated is : ")
print(N)

my_result = tuple((elem, ) * N for elem in range(1, 6))
print("The tuple sequence is : ")
print(my_result)

出力

The value of 'N' has been initialized
The number of times it has to be repeated is :
3
The tuple sequence is :
((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))

説明

  • 「N」の値が初期化され、コンソールに表示されます。
  • 特定の要素を持ち、特定の範囲で「N」回乗算されたタプルが変数に割り当てられます。
  • このシーケンスが生成され、コンソールに出力として表示されます。

  1. タプル内の要素の出現をカウントするPythonプログラム

    ここでユーザー入力タプルが与えられた場合、私たちのタスクはタプル内の特定の要素の出現をカウントすることです。 例 Input : A = [10, 20, 30, 40, 10, 100, 80, 10] X = 10 Output : 3 アルゴリズム countoccur(A,x) /* A is an array and x is the element to count the number of occurrences */ Step 1: First we use one counter variable which is count the same ele

  2. Pythonタプル

    Pythonでは、タプルは順序付けられたコレクションです。 および不変 。つまり、タプルにアイテムを追加したり、タプルからアイテムを削除したりすることはできません。 括弧()を使用してタプルを作成します および少なくとも1つのコンマ( , ) 。 タプルは、スライスの結果もタプルになることを除いて、リストと同じようにインデックス付けおよびスライスできます。 タプルを作成する方法 colorsTuple = (red, green, blue) print(colorsTuple) 出力: (red, green, blue) 1つのアイテムのみでタプルを作成する タプルには少なくと