tuple【タプル型】3.5
メソッド 一覧 コンストラクタ タプル表現 繰り返し処理 パック・アンパック 共通 シーケンス演算
メモ
- タプル型
- イミュータブル(変更不可) なシーケンス
(ミュータブルなシーケンス:list【リスト型】) - list【リスト型】で可能な内包表記 は不可 (ジェネレータ式となる) 〔 例 〕
(リスト内包表記をコンストラクタに指定すれば同等な生成可能) - 要素数:組み込み関数の len()【長さ・要素数】 〔 例 〕
- 重複可・入れ子可 〔 例 〕
- 各要素の型は違っても可 〔 例 〕
- C系言語の構造体の代替 (名前ではなくインデックスでアクセス) 〔 例 〕
- タプル表現の末尾カンマは無視 (1要素の場合、カンマ必須)
- ソートは組み込み関数の sorted()【ソート】 参照
- 変更不可の為、辞書のキーとして使用可
- イミュータブル(変更不可) なシーケンス
- 繰り返し処理 (for-in【for ループ】 で処理)〔 例 〕
使用関数・メソッド 備考 なし 要素を取得して処理 enumerate()【番号付け】 インデックスと要素を取得して処理 zip()【組分け】 複数のシーケンス型の要素を取得し処理 (一度に最小要素数分処理)
(list【リスト型】のリスト内包表記 と組合せて、要素を元にタプル生成可)range【レンジ型】 要素をインデックスで取得し処理 (複数のシーケンス型を一度に最大要素数分処理 等) reversed()【逆順】 逆順に要素を取得して処理 sorted()【ソート】 ソート順に要素を取得して処理 - パック・アンパック 〔 例 〕
区分 対象 備考 パック 丸カッコ無しのカンマ区切りの要素はタプルにパック
(タプル表現・コンストラクタ 参照)関数の複数戻り値指定 (return【関数の戻り値】 参照)
タプルへ代入 (例:tpl = 1, 2)
複数同時代入の右辺 (例:x, y = 1, 2)アンパック 複数変数に代入(右辺) 3.0 代入文の右辺でアンパックして代入 複数変数に代入(左辺) 3.0 上記右辺を同数の変数指定のタプル(丸カッコ省略可)に代入
要素数が違う場合は ValueError 例外
アスタリスク(*)指定の変数には過分要素が list【リスト型】で代入
不要な値はアンダースコア( _ ) のみの変数に代入すれば明確
下記で使用
複数同時代入の左辺 (例:x, y = 1, 2)
関数の複数戻り値の取得タプル表現
リスト表現
リスト内包表記
等アスタリスク(*)指定位置で個別要素に展開 3.5 関数の位置引数 引数のアスタリスク(*)指定位置で個別要素に展開 (複数指定可 3.5 ) - メソッド 一覧
メソッド 関連 コンストラクタ タプル表現 count()【要素出現回数】 index()【要素インデックス】 - 共通 シーケンス演算 ( 共通のシーケンス演算) 〔 例 〕
該当タプルの変更なし 〔 例 〕 【指定値】
i・j<0
i・j が省略 または None
i・j が範囲外【特記なしの動作】
末尾からのインデックス ( len(tuple) + i・j :末尾が -1 )
先頭 または 末尾 (k の正負により範囲が広がる位置)
先頭 または 末尾 (近いほう)演算 備考 x in tuple タプル tuple のある要素が x と等しければ True 、等しくなければ False x not in tuple タプル tuple のある要素が x と等しければ False 、等しくなければ True tuple1 + tuple2 タプル tuple1 と タプル tuple2 の結合 tuple * n
n * tupleタプル tuple を n(整数) 回結合 (n<0:空のタプル)
注:参照のコピーtuple[i ] タプル tuple の i 番目の要素
範囲外:IndexError 例外tuple[[i] :[j]] タプル tuple の i~j(含まない) 番目までの要素 tuple[[i] :[j] :[k]] タプル tuple の i~j(含まない) 番目までの k(省略:1) 番目 毎の要素
0<k:先頭から末尾方向
k<0:末尾から先頭方向・i は末尾側を指定 (範囲補正なし)
k=0:不可 (ValueError 例外)len(tuple) タプル tuple の要素数 max(tuple) タプル tuple の最大要素 min(tuple) タプル tuple の最小要素 tuple.index(x [, i [, j ]]) タプル tuple 中で 要素 x が i~j(含まない) の間で 最初に出現する位置インデックス
未検出:ValueError 例外tuple.count(x) タプル tuple 中に 要素 x が出現する回数 - 外部リンク
例 (メモ)
# 要素数
tpl = (1, 2, 3, 4, 5)
print(len(tpl)) # 出力:5
# 重複
tpl = (1, 1, 1, 1, 1)
print(len(tpl)) # 出力:5
# 入れ子
tpl = (1, 2, (31, 32, 33), 4, 5)
print(len(tpl)) # 出力:5
print(len(tpl[2])) # 出力:3
# 要素の型
tpl = (1, 2.0, "3", True, [1, 2, 3], (1, 2, 3))
print(len(tpl)) # 出力:6
print(len(tpl[4])) # 出力:3
print(len(tpl[5])) # 出力:3
# 構造体の代替
ID = 0
NAME = 1
ADDRESS = 2
tpl1 = (1, "Yamada Taro", "Tokyo")
tpl2 = (2, "Tanaka Hanako", "Osaka")
print(tpl1[ID]) # 出力:1
print(tpl1[NAME]) # 出力:Yamada Taro
print(tpl1[ADDRESS]) # 出力:Tokyo
print(tpl2[ID]) # 出力:2
print(tpl2[NAME]) # 出力:Tanaka Hanako
print(tpl2[ADDRESS]) # 出力:Osaka
lst = [tpl1, tpl2]
for item in lst:
print(F"Id={item[ID]} Name='{item[NAME]}' Address='{item[ADDRESS]}'")
# 出力:Id=1 Name='Yamada Taro' Address='Tokyo'
# 出力:Id=2 Name='Tanaka Hanako' Address='Osaka'
例 (繰り返し処理)
tpl = ('a', 'b', 'c')
for item in tpl:
print(item)
# 出力:a
# 出力:b
# 出力:c
tpl = ('a', 'b', 'c')
for index, item in enumerate(tpl):
print(index, item)
# 出力:0 a
# 出力:1 b
# 出力:2 c
# zip
tpl1 = ('a', 'b', 'c')
tpl2 = ('A', 'B', 'C', 'D')
for item1, item2 in zip(tpl1, tpl2):
print(item1 + item2)
# 出力:aA
# 出力:bB
# 出力:cC
# zip + リスト内包表記
tpl_a = (1, 2, 3, 4)
tpl_b = (2, 3, 4, 5)
tpl_c = tuple([x + y for x, y in zip(tpl_a, tpl_b)])
tpl_d = tuple([x * y for x, y in zip(tpl_a, tpl_b)])
print(tpl_a)
# 出力:(1, 2, 3, 4)
print(tpl_b)
# 出力:(2, 3, 4, 5)
print(tpl_c)
# 出力:(3, 5, 7, 9)
print(tpl_d)
# 出力:(2, 6, 12, 20)
tpl1 = ('a', 'b', 'c')
tpl2 = ('A', 'B', 'C', 'D')
len1 = len(tpl1)
len2 = len(tpl2)
length = max(len1, len2)
for i in range(length):
if len1 <= i:
x = "-"
else:
x = tpl1[i]
if len2 <= i:
y = "-"
else:
y = tpl2[i]
print(i, x + y)
# 出力:0 aA
# 出力:1 bB
# 出力:2 cC
# 出力:3 -D
tpl = ('a', 'b', 'c')
for item in reversed(tpl):
print(item)
# 出力:c
# 出力:b
# 出力:a
tpl = ('a', 'c', 'b')
for item in sorted(tpl):
print(item)
# 出力:a
# 出力:b
# 出力:c
例 (パック・アンパック)
# パック
def func(x):
return x, x*2, x*3
tpl = func(2)
print(tpl) # 出力:(2, 4, 6)
(x, y, z) = func(2)
print(x, y, z) # 出力:2 4 6
x, y, z = func(3)
print(x, y, z) # 出力:3 6 9
tpl = 1,
print(tpl) # 出力:(1,)
tpl = 1, 2
print(tpl) # 出力:(1, 2)
x, y = 1, 2
print(x, y) # 出力:1 2
# アンパック:複数変数に代入(右辺)
(x, y, z) = (1, 3, 5)
print(x, y, z) # 出力:1 3 5
x, y, z = (2, 4, 6)
print(x, y, z) # 出力:2 4 6
# アンパック:複数変数に代入(左辺)
tpl = (10, 20)
x, y = tpl
print(x, y) # 出力:10 20
#x, = tpl # ValueError 例外
#x, y, z = tpl # ValueError 例外
tpl = (1, 2, 3, 4)
*x, y, z = tpl
print(x, y, z) # 出力:[1, 2] 3 4
x, *y, z = tpl
print(x, y, z) # 出力:1 [2, 3] 4
x, y, *z = tpl
print(x, y, z) # 出力:1 2 [3, 4]
*x, y = tpl
print(x, y) # 出力:[1, 2, 3] 4
x, *y = tpl
print(x, y) # 出力:1 [2, 3, 4]
*x, = tpl
print(x) # 出力:[1, 2, 3, 4]
*x1, x2, x3, x4, x5 = tpl
print(x1, x2, x3, x4, x5) # 出力:[] 1 2 3 4
x, y, z, _ = tpl
print(x, y, z) # 出力:1 2 3
x, *_, z = tpl
print(x, z) # 出力:1 4
x, *_ = tpl
print(x) # 出力:1
# アンパック:シーケンス等のリテラル要素
tpl0 = (21, 22, 23)
lst = [1, tpl0, 3]
print(lst) # 出力[1, (21, 22, 23), 3]:
lst = [1, *tpl0, 3]
print(lst) # 出力:[1, 21, 22, 23, 3]
tpl = (1, tpl0, 3)
print(tpl) # 出力:(1, (21, 22, 23), 3)
tpl = (1, *tpl0, 3)
print(tpl) # 出力:(1, 21, 22, 23, 3)
# アンパック:シーケンス等のリテラル要素
tpl = (10, -20, 30)
#print(max(1, tpl, 3)) # TypeError 例外
print(max(1, *tpl, 3)) # 出力:30
#print(min(1, tpl, 3)) # TypeError 例外
print(min(1, *tpl, 3)) # # 出力:-20
例 (共通 シーケンス演算)
tpl = (1, 2, 3)
# x in tuple
print(1 in tpl) # 出力:True
print(4 in tpl) # 出力:False
# x not in tuple
tpl = (1, 2, 3)
print(1 not in tpl) # 出力:False
print(4 not in tpl) # 出力:True
# tuple1 + tuple2
tpl1 = (1, 2, 3)
tpl2 = (4, 5, 6)
tpl3 = tpl1 + tpl2
print(tpl3) # 出力:(1, 2, 3, 4, 5, 6)
print(tpl1 + ()) # 出力:(1, 2, 3)
print(() + tpl1) # 出力:(1, 2, 3)
# tuple*n
# n*tuple
tpl = (1, 2, 3)
tpl1 = tpl * 3
print(tpl1) # 出力:(1, 2, 3, 1, 2, 3, 1, 2, 3)
tpl2 = 3 * tpl
print(tpl2) # 出力:(1, 2, 3, 1, 2, 3, 1, 2, 3)
tpl3 = tpl * (-3)
print(tpl3) # 出力:()
tpl4 = ([],) * 3
print(tpl4) # 出力:([], [], [])
tpl4[0].append(9)
print(tpl4) # 出力:([9], [9], [9])
lst = [11, 12]
tpl = (0, lst)
tpl5 = tpl * 3
print(tpl5) # 出力:(0, [11, 12], 0, [11, 12], 0, [11, 12])
tpl5[1].append(9)
print(tpl5) # 出力:(0, [11, 12, 9], 0, [11, 12, 9], 0, [11, 12, 9])
print(lst) # 出力:[11, 12, 9]
# tuple(i)
tpl = (0, 1, 2, 3, 4, 5, 6)
print(tpl[3]) # 出力:3
print(tpl[-2]) # 出力:5
#print(tpl[99]) # IndexError 例外
#print(tpl[-99]) # IndexError 例外
# tuple(i:j)
tpl = (0, 1, 2, 3, 4, 5, 6)
print(tpl[2:4]) # 出力:(2, 3)
print(tpl[2:-3]) # 出力:(2, 3)
print(tpl[-5:-3]) # 出力:(2, 3)
print(tpl[:4]) # 出力:(0, 1, 2, 3)
print(tpl[2:]) # 出力:(2, 3, 4, 5, 6)
print(tpl[:]) # 出力:(0, 1, 2, 3, 4, 5, 6)
print(tpl[-99:4]) # 出力:(0, 1, 2, 3)
print(tpl[2:99]) # 出力:(2, 3, 4, 5, 6)
print(tpl[-99:99]) # 出力:(0, 1, 2, 3, 4, 5, 6)
# tuple(i:j:k)
tpl = (0, 1, 2, 3, 4, 5, 6)
print(tpl[1:6:2]) # 出力:(1, 3, 5)
print(tpl[1:-1:2]) # 出力:(1, 3, 5)
print(tpl[-6:-1:2]) # 出力:(1, 3, 5)
print(tpl[-99:99:2]) # 出力:(0, 2, 4, 6)
print(tpl[5:1:-2]) # 出力:(5, 3)
print(tpl[-2:1:-2]) # 出力:(5, 3)
print(tpl[-2:-6:-2]) # 出力:(5, 3)
print(tpl[1:5:-2]) # 出力:()
#print(tpl[5:1:0]) # ValueError 例外
print(tpl[:6:2]) # 出力:(0, 2, 4)
print(tpl[1::2]) # 出力:(1, 3, 5)
print(tpl[1:6:]) # 出力:(1, 2, 3, 4, 5)
print(tpl[::2]) # 出力:(0, 2, 4, 6)
print(tpl[1::]) # 出力:(1, 2, 3, 4, 5, 6)
print(tpl[::]) # 出力:(0, 1, 2, 3, 4, 5, 6)
# len(tuple)
tpl = (1, 2, 3, 4)
print(len(tpl)) # 出力:4
print(len(())) # 出力:0
tpl = (1, -2, 3, -4)
# min(tuple)
print(min(tpl)) # 出力:-4
# max(tuple)
print(max(tpl)) # 出力:3
# tuple.index(x(,i(,j)))
tpl = (0, 1, 2, 3, 4, 3, 2, 1, 0)
print(tpl.index(1))
# 出力:1
print(tpl.index(1, 2))
# 出力:7
print(tpl.index(1, 2, 8))
# 出力:7
#print(tpl.index(1, 2, 7)) # ValueError 例外
print(tpl.index(1, -99))
# 出力:1
print(tpl.index(1, 2, 99))
# 出力:7
print(tpl.index(1, -99, 99))
# 出力:1
# tuple.count(x)
tpl = (1, 2, 3, 2, 1)
print(tpl.count(1)) # 出力:2
print(tpl.count(3)) # 出力:1
print(tpl.count(0)) # 出力:0
タプル表現・コンストラクタ
構文
()
(item1,)
(item1, ...itemN[,])
item1,
item1, ...itemN[,]
class tuple( [iterable] )
itemN要素項目
iterable元になるイテラブルオブジェクト (シーケンス・イテレートサポートのコンテナ・イテレータオブジェクト)
※:タプル表現の末尾カンマは無視 (空要素にはならない)
例
# タプル表現
tpl = ()
print(tpl) # 出力:()
tpl = (1)
print(tpl) # 出力:1 【数値】
tpl = (1,)
print(tpl) # 出力:(1,)
tpl = (1, 2, 3)
print(tpl) # 出力:(1, 2, 3)
tpl = 1,
print(tpl) # 出力:(1)
tpl = 1, 2, 3
print(tpl) # 出力:(1, 2, 3)
tpl = (1, (21, 22), (31, (321, 322)))
print(tpl)
# 出力:(1, (21, 22), (31, (321, 322)))
tpl = (1, '2', 3.0)
print(tpl)
# 出力:(1, '2', 3.0)
tpl = (1, 1, 1)
print(tpl)
# 出力:(1, 1, 1)
# コンストラクタ
tpl = tuple()
print(tpl)
# 出力:()
tpl = tuple((1, 3, 5))
print(tpl)
# 出力:(1, 3, 5)
tpl = tuple((2, 4, 6))
print(tpl)
# 出力:(2, 4, 6)
tpl = tuple("abc")
print(tpl)
# 出力:('a', 'b', 'c')
# リスト内包表記の使用
dummy = (x for x in range(5)) # ジェネレータ式
print(type(dummy)) # 出力:<class 'generator'>
tpl = tuple([x for x in range(5)])
print(tpl) # 出力:(0, 1, 2, 3, 4)
tpl = tuple([x * 3 for x in range(5)])
print(tpl) # 出力:(0, 3, 6, 9, 12)
tpl = tuple([x for x in range(10) if x % 2 == 0])
print(tpl) # 出力:(0, 2, 4, 6, 8)
tpl = tuple([x * 3 for x in range(10) if x % 2 == 0])
print(tpl) # 出力:(0, 6, 12, 18, 24)