タプルも複数の値をまとめたものだが、リストと違うのは、値の変更など様々な操作が出来ない点である。
とほほのPython入門 - リスト・タプル・辞書 - とほほのWWW入門
値をカンマで区切って、括弧"()"で括るとタプルになる。
#!/usr/bin/python lst = [10, 20, 30] print "lst = %s" % lst lst[1] = 15 print "lst = %s" % lst tpl = (100, 200, 300) print tpl[0] print tpl[1] print tpl[2] tpl[1] = 150
としても、
$ vi tuple.py
$ python tuple.py
lst = [10, 20, 30]
lst = [10, 15, 30]
100
200
300
Traceback (most recent call last):
File "tuple.py", line 17, in <module>
tpl[1] = 150
TypeError: 'tuple' object does not support item assignment
$エラーになる。
で、
$ vi tuple.py $ python tuple.py lst = [10, 20, 30] lst = [10, 15, 30] 100 200 300 $ ls
諦めた。
まあリストに変換できるので、そうすれば変更できるらしい。
Sample/python/tuple/tuple/src/tuple at master · bg1bgst333/Sample · GitHub