Python 配列(タプル)

他のプログラミング言語で「配列」への代入がありますが、Pythonでは「リスト」と「タプル」の2種類があります。
Pythonの「リスト」に関しては「Python 配列(リスト)」を参照してください。
今回はPythonでの配列である「タプル」を解説します。

この記事の著者 ->S.E; です。
メインとしている言語はPHPですが、この先、機械学習やAIなどで利用されているPythonを習得したいと考えています。
このページは「ゼロから始めるPythonプログラミング学習」としてPythonの基本的な部分から学習を始めたいと思います。
最終的にはWebでの利用やアプリの開発、機械学習、AI、簡単なゲームなどのプログラミングを目指したいと思います。
また、参考になる書籍なども紹介しています。
配列とは?
変数では、例えば変数「str」に対して、文字列や整数など1つの情報しか格納することができませんが、配列の場合は変数「str」にたいして複数の情報を格納し呼び出すことができます。
タプル
Pyhtonの配列の一つである「タプル」について解説します。
変数に情報をリストとして格納するには「(…)」を利用し、カンマ区切りで情報を格納します。
格納する情報は整数でも文字列でも構いません。(文字列の場合は「‘」もしくは「“」で文字列を囲む必要があります。)
変数に格納した情報を呼び出す場合は変数名の後に「[]」を記述し呼び出したいインデックス番号を記述します。
この時、変数に格納された最初の情報は「0(ゼロ)」となります。
1 2 3 4 5 | >>> tuple = (1,2,3,4,'文字列') >>> tuple[0] 1 // 結果 >>> tuple[4] '文字列' // 結果 |
リストとタプルの違いは?
リストは定義した値を変更できるのに対してタプルは一度定義した値を変更することができません。
変数「list」にはリストを変数「tuple」にはタプルを定義し、のちに配列の一部の値を変更しています。
リストは変更できたのに対してタプルは「TypeError: ‘tuple’ object does not support item assignment」のエラーが返されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> list = [1,2,3] >>> tuple = (1,2,3) >>> list [1, 2, 3] >>> tuple (1, 2, 3) >>> list[0] = 4 >>> list [4, 2, 3] >>> tuple[0] = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment |
情報の削除
del文
タプル内の情報を削除する場合はdel文を利用しますが、インデックス番号で場所を指定した削除方法はエラーとなり利用できません。
リストではdel 変数名[削除するインデックス番号] で任意の情報を削除することができましたが、タプルの場合は以下のようにエラーとなります。
1 2 3 4 5 | >>> tuple = (1,2,3,4,'文字列') >>> del tuple[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object doesn't support item deletion |
インデックス番号を指定するブラケット「[]」を記述せずタプルないの情報を全て削除することは可能です。
1 2 3 4 | >>> tuple = (1,2,3,4,'文字列') >>> del tuple >>> tuple <class 'tuple'> |
情報の検索
index()関数
index()関数を利用すると、指定した情報のインデックス番号を返してくれます。
数名.index(探す情報) で検索することができます。指定した情報が複数存在する場合は、タプルのインデックス番号が若い(最初の情報)もののインデックス番号が返されます。
1 2 3 | >>> tupel = [1,2,3,1,2,3] >>> tuple.index(2) 1 |
count()関数
count()関数はリスト内に指定した情報がいくつあるかを返してくれます。
数名.count(探す情報) で件数を数えます。
1 2 3 | >>> tuple = [1,2,3,1,2,3] >>> tuple.count(2) 2 |
その他の関数
「タプル」で利用できる関数は多数あります。どの様な関すがあるかを調べるのにhelp()関すが用意されています。
今回解説したindex()やcount()が含まれています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | >>> help(tuple) class tuple(object) | tuple(iterable=(), /) | | Built-in immutable sequence. | | If no argument is given, the constructor returns an empty tuple. | If iterable is specified the tuple is initialized from iterable's items. | | If the argument is a tuple, the return value is the same object. | | Built-in subclasses: | asyncgen_hooks | UnraisableHookArgs | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(self, key, /) | Return self[key]. | | __getnewargs__(self, /) | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value. | | __ne__(self, value, /) | Return self!=value. | | __repr__(self, /) | Return repr(self). | | __rmul__(self, value, /) | Return value*self. | | count(self, value, /) | Return number of occurrences of value. | | index(self, value, start=0, stop=9223372036854775807, /) | Return first index of value. | | Raises ValueError if the value is not present. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. |
S.E->お勧め記事;
- CrowdWorks 提案後、受注に繋がりました。提案〜契約までの流れを解説
- Panic Nova 購入から1年が過ぎライセンスの更新時期の注意点
- PHPフレームワーク「Laravel」PHPテンプレートを利用する
- CrowdWorks いつの間にか「プロクラウドワーカー」になっていた
- macOS Monterey にアップデート後、composerやhomebrewでenv: php: No such file or directoryが出る
- Shopify APIを利用して在庫管理を行う
- PHP フレームワーク Laravel ディレクティブ – ループ変数 $loop –
- プログラマー になる為に必要な プログラミング以外 の知識・スキル
- PHPフレームワーク「Laravel」Bladeテンプレートを利用する
S.E->PR;
チームのタスク管理ツール【backlog】
FREENANCE(フリーナンス)
S.E->Weekly Ranking;
S.E->プロフィール;

