タプルは Python の重要なデータ構造であり、順序付けされた不変のデータ コレクションを保存する便利な方法を提供します。
このブログでは、作成、スライス、メソッドなどを含む、Python のタプルに関するすべてを学びます。
早速始めましょう!?
タプルはデータ項目の順序付けされたコレクションです。タプルでは、複数の項目を 1 つの変数に格納できます。
タプルは不変です。つまり、作成後に変更することはできません。
タプルは丸括弧 () を使用して定義され、項目はカンマで区切られます。
タプルには、さまざまなデータ型の項目を含めることができます。
例:
tuple1 = (1,2,36,3,15) tuple2 = ("Red", "Yellow", "Blue") tuple3 = (1, "John",12, 5.3) print(tuple1) # (1, 2, 36, 3, 15) print(tuple2) # ('Red', 'Yellow', 'Blue') print(tuple3) # (1, 'John', 12, 5.3)
1 つの項目を含むタプルを作成するには、項目の後にカンマを追加します。カンマがないと、Python はそれを整数型として扱います。
例:
tuple1 = (1) # This is an integer. print(type(tuple1)) # <class 'int'> tuple2 = (1,) # This is a tuple. print(type(tuple2)) # <class 'tuple'>
len() 関数を使用して、タプルの長さ (タプル内の項目の数) を見つけることができます。
例:
tuple1 = (1,2,36,3,15) lengthOfTuple = len(tuple1) print(lengthOfTuple) # 5
インデックスを使用してタプル項目/要素にアクセスできます。各要素には独自のインデックスがあります。
インデックス付けは、最初の要素は 0、2 番目の要素は 1 から始まります。
例:
fruits = ("Orange", "Apple", "Banana") print(fruits[0]) # Orange print(fruits[1]) # Apple print(fruits[2]) # Banana
タプルの末尾から要素にアクセスすることもできます (最後の要素の場合は -1、最後から 2 番目の要素の場合は -2 など)。これは ネガティブ インデックス と呼ばれます。
例:
fruits = ("Orange", "Apple", "Banana") print(fruits[-1]) # Banana print(fruits[-2]) # Apple print(fruits[-3]) # Orange # for understanding, you can consider this as fruits[len(fruits)-3]
in キーワードを使用して、要素がタプルに存在するかどうかを確認できます。
例 1:
fruits = ("Orange", "Apple", "Banana") if "Orange" in fruits: print("Orange is in the tuple.") else: print("Orange is not in the tuple.") #Output: Orange is in the tuple.
例 2:
numbers = (1, 57, 13) if 7 in numbers: print("7 is in the tuple.") else: print("7 is not in the tuple.") # Output: 7 is not in the tuple.
start、end、jump(skip) パラメータを指定することで、タプル項目の範囲を取得できます。
構文:
tupleName[start : end : jumpIndex]
注: ジャンプインデックスはオプションです。
例 1:
# Printing elements within a particular range numbers = (1, 57, 13, 6, 18, 54) # using positive indexes(this will print the items starting from index 2 and ending at index 4 i.e. (5-1)) print(numbers[2:5]) # using negative indexes(this will print the items starting from index -5 and ending at index -3 i.e. (-2-1)) print(numbers[-5:-2])
出力:
(13, 6, 18) (57, 13, 6)
例 2:
終了インデックスが指定されていない場合、インタープリターは最後まですべての値を出力します。
# Printing all elements from a given index to till the end numbers = (1, 57, 13, 6, 18, 54) # using positive indexes print(numbers[2:]) # using negative indexes print(numbers[-5:])
出力:
(13, 6, 18, 54) (57, 13, 6, 18, 54)
例 3:
開始インデックスが指定されていない場合、インタープリタは開始インデックスから指定された終了インデックスまでのすべての値を出力します。
# Printing all elements from start to a given index numbers = (1, 57, 13, 6, 18, 54) #using positive indexes print(numbers[:4]) #using negative indexes print(numbers[:-2])
出力:
(1, 57, 13, 6) (1, 57, 13, 6)
例 4:
ジャンプインデックスを指定すると、代替値を出力できます。
# Printing alternate values numbers = (1, 57, 13, 6, 18, 54) # using positive indexes(here start and end indexes are not given and 2 is jump index.) print(numbers[::2]) # using negative indexes(here start index is -2, end index is not given and 2 is jump index.) print(numbers[-2::2])
出力:
(1, 13, 18) (18)
タプルは不変であるため、項目を追加、削除、変更することはできません。ただし、タプルを リスト に変換し、リストを変更して、タプルに戻すことはできます。
例:
tuple1 = (1,2,36,3,15) tuple2 = ("Red", "Yellow", "Blue") tuple3 = (1, "John",12, 5.3) print(tuple1) # (1, 2, 36, 3, 15) print(tuple2) # ('Red', 'Yellow', 'Blue') print(tuple3) # (1, 'John', 12, 5.3)
演算子を使用して 2 つのタプルを結合できます。
例:
tuple1 = (1) # This is an integer. print(type(tuple1)) # <class 'int'> tuple2 = (1,) # This is a tuple. print(type(tuple2)) # <class 'tuple'>
出力:
tuple1 = (1,2,36,3,15) lengthOfTuple = len(tuple1) print(lengthOfTuple) # 5
タプルには次の組み込みメソッドがあります:
このメソッドは、タプル内に要素が出現する回数を返します。
構文:
fruits = ("Orange", "Apple", "Banana") print(fruits[0]) # Orange print(fruits[1]) # Apple print(fruits[2]) # Banana
例:
fruits = ("Orange", "Apple", "Banana") print(fruits[-1]) # Banana print(fruits[-2]) # Apple print(fruits[-3]) # Orange # for understanding, you can consider this as fruits[len(fruits)-3]
このメソッドは、タプルから指定された要素の最初の出現を返します。
注: タプル内に要素が見つからない場合、このメソッドは ValueError を生成します。
例:
fruits = ("Orange", "Apple", "Banana") if "Orange" in fruits: print("Orange is in the tuple.") else: print("Orange is not in the tuple.") #Output: Orange is in the tuple.
検索の 開始インデックス を指定できます。例:
numbers = (1, 57, 13) if 7 in numbers: print("7 is in the tuple.") else: print("7 is not in the tuple.") # Output: 7 is not in the tuple.
今日はここまでです。
お役に立てば幸いです。
読んでいただきありがとうございます。
言語を学習中に Python の詳細なメモを作成しました。これらはわずか $1 で入手できます。ここから入手してください: 今すぐダウンロード
このようなコンテンツをさらにご覧になりたい場合は、ここをクリックしてください。
X(Twitter) で私をフォローして、毎日の Web 開発のヒントを入手してください。
コーディングを続けてください!!
以上がPython でタプルをマスターする: 包括的なガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。