Python/タプル

出典: フリー教科書『ウィキブックス(Wikibooks)』

タプル[編集]

タプル( tuple )は、任意のオブジェクトをゼロ個以上、順次付けて保持できるコレクションです[1]。 リストとよく似た特徴がありますが、リストと違い生成後は内容が変更できない「イミュータブル」なオブジェクトです。

タプルリテラル[編集]

タプルリテラル
tpl0 = ()
tpl1 = (10,)
tpl2 = 10, "ABC", 30
tpl3 = (1,"one"), (2,"two"),(3,"three")
tpl4 = (
    (1,"壱"),
    (2,"弐"),
    (3,"参")
    )
print(tpl0)
print(tpl1)
print(tpl2)
print(tpl3)
print(tpl4)
実行結果
()
(10,)
(10, 'ABC', 30)
((1, 'one'), (2, 'two'), (3, 'three')) 
((1, '壱'), (2, '弐'), (3, '参'))
空のタプルは、空の括弧の組で作ることができます。
1つの項目からなるタプル(「シングルトン」(singleton))は、式にカンマを付けることで形成されます(式はそれ自体ではタプルを作らないので、括弧 ( ) は式のグループ化のために使用可能でなければならないからです)。
2つ以上の項目からなるタプルリテラルは、カンマで区切られた式のリストで形成されます。
タプルリテラルに限らず、式を括弧 ( ) で括ると複数行に渡って書くことができます。

タプルと演算子[編集]

タプルと演算子
tpl = 10 ,"ABC" ,30
print(f'''\
{tpl=}
{tpl[1]=}
{tpl[-1]=}
{tpl + (2,3,4)=}
{tpl * 2=}
{2 * tpl=}
{tpl == (10 ,"ABC" ,30)=}
{tpl != (10 ,"ABC" ,30)=}
{tpl < (10 ,"ABC" ,30)=}
{tpl > (10 ,"ABC" ,30)=}
''')
実行結果
tpl=(10, 'ABC', 30)
tpl[1]='ABC'
tpl[-1]=30
tpl + (2,3,4)=(10, 'ABC', 30, 2, 3, 4)
tpl * 2=(10, 'ABC', 30, 10, 'ABC', 30)
2 * tpl=(10, 'ABC', 30, 10, 'ABC', 30)
tpl == (10 ,"ABC" ,30)=True
tpl != (10 ,"ABC" ,30)=False
tpl < (10 ,"ABC" ,30)=False
tpl > (10 ,"ABC" ,30)=False
タプルの要素は、インデックス演算子で参照できますが、左辺値は得られません(イミュータブルなので)。
タプル内の要素の順位はリストと同じく、0番から始まります。
負のインデクスiは、len(self)+iを表します。
タプル同士の足し算は、連結した新しいタプルを返します。
タプルtと整数nとの乗算は、tをn回繰り返したタプルを返します。
整数nとタプルtとの乗算は、tをn回繰り返したタプルを返します。
タプル同士は、比較や大小判別ができます。

タプルのフィールド一覧[編集]

タプルのフィールド一覧
obj = tuple()
for i in dir(obj):
    print(i, eval(f"type({obj}.{i})"))
実行結果
__add__ <class 'method-wrapper'>
__class__ <class 'type'>
__contains__ <class 'method-wrapper'>
__delattr__ <class 'method-wrapper'>
__dir__ <class 'builtin_function_or_method'>
__doc__ <class 'str'>
__eq__ <class 'method-wrapper'>
__format__ <class 'builtin_function_or_method'>
__ge__ <class 'method-wrapper'>
__getattribute__ <class 'method-wrapper'>
__getitem__ <class 'method-wrapper'>
__getnewargs__ <class 'builtin_function_or_method'>
__gt__ <class 'method-wrapper'>
__hash__ <class 'method-wrapper'>
__init__ <class 'method-wrapper'>
__init_subclass__ <class 'builtin_function_or_method'>
__iter__ <class 'method-wrapper'>
__le__ <class 'method-wrapper'>
__len__ <class 'method-wrapper'>
__lt__ <class 'method-wrapper'>
__mul__ <class 'method-wrapper'>
__ne__ <class 'method-wrapper'>
__new__ <class 'builtin_function_or_method'>
__reduce__ <class 'builtin_function_or_method'>
__reduce_ex__ <class 'builtin_function_or_method'>
__repr__ <class 'method-wrapper'>
__rmul__ <class 'method-wrapper'>
__setattr__ <class 'method-wrapper'>
__sizeof__ <class 'builtin_function_or_method'>
__str__ <class 'method-wrapper'>
__subclasshook__ <class 'builtin_function_or_method'>
count <class 'builtin_function_or_method'>
index <class 'builtin_function_or_method'>

メソッドセットはリストに準じますが、イミュータブルなので append clear copy extend insert pop remove reverse sort の9つのメソッドはありません。


脚註[編集]

  1. ^ 3.10.1 Documentation » The Python Language Reference » 3. Data model ¶3.2. The standard type hierarchy” (2021年12月16日). 2021年12月16日閲覧。