hirhirの日記

Pythonとデータ分析と人口知能

Python3 入門:ネイティブデータ型 タプル

Python3 入門 タプル

タプルとは?

ネイティブデータ型 - Dive Into Python 3 日本語版

タプルはイミュータブルなリストだ。いったん作成されたタプルは、どんな手段によっても変更できない。

らしい,まあ不変のリストのイメージだけど違うらしい連結はできるみたい

リストとの相互変換可能とのこと

 

では今回もipython notebookにて いやー便利だわnotebook

In [1]:
 
a=('a',"b",'c',"c")
a
 
 
Out[1]:
('a', 'b', 'c', 'c')
In [3]:
 
a[1]
 
 
Out[3]:
'b'
In [4]:
 
a[-1]
 
 
Out[4]:
'c'
In [5]:
 
a[1:3]
 
 
Out[5]:
('b', 'c')

indexが使えてスライスすればタプルを返す。

まぁリストと変わらない。

In [1]:
 
a=('a','b','c','d','e')
a
 
 
Out[1]:
('a', 'b', 'c', 'd', 'e')
In [2]:
 
a.append("f")
 
 
 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-0a7fa197c53b> in <module>()
----> 1 a.append("f")

AttributeError: 'tuple' object has no attribute 'append'

In [3]:
 
a.extend([1])
 
 
 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-753518d57520> in <module>()
----> 1 a.extend([1])

AttributeError: 'tuple' object has no attribute 'extend'

In [4]:
 
a.pop(1)
 
 
 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-5fb08b80da19> in <module>()
----> 1 a.pop(1)

AttributeError: 'tuple' object has no attribute 'pop'

In [5]:
 
a.remove(-1)
 
 
 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-89080a03c5c6> in <module>()
----> 1 a.remove(-1)

AttributeError: 'tuple' object has no attribute 'remove'

In [7]:
 
a.index('a')
 
 
Out[7]:
0
In [8]:
 
a.index(0)
 
 
 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-8b50e151dfc5> in <module>()
----> 1 a.index(0)

ValueError: tuple.index(x): x not in tuple

なるほどタプルは

append,extend,pop,removeは変更ができないので持っていない

indexは参照なのでOK

存在しないならErrorを返却する。

でも連結はできる

In [16]:
 
a=(1,2,3,4,5)
b=(6,7,8,9,0)
c=a+b
c
 
 
Out[16]:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)

関数はないけど

不変性ではない、、、ないんかい!って突っ込んだ

 

相互変換

In [2]:
 
a=('a','b','c','d','e')
list(a)
 
 
Out[2]:
['a', 'b', 'c', 'd', 'e']
In [3]:
 
tuple(a)
 
 
Out[3]:
('a', 'b', 'c', 'd', 'e')

listとtupleの相互変換ができる

リストの凍結と解凍ってところですなー

 

そしてtupleは参照がlistより高速らしい、、、

以下にて検証してみた

 

In [8]:
 
import time
 
base = [i for i in range(1000000)]
tmplist = [i for i in range(1000000)]
 
start = time.clock()
tmp = []
for i in base:
  tmp = tmplist[i]
 
end = time.clock()
print(end - start)
 
 
 
0.45567199999999985
In [9]:
 
import time
 
base = tuple([i for i in range(1000000)])
tmplist = tuple([i for i in range(1000000)])
 
start = time.clock()
tmp = 0
for i in base:
  tmp = tmplist[i]
 
end = time.clock()
print(end - start)
 
 
 
0.1010339999999994

 

なるほど高速みたい

 しかし

In [12]:
 
import time
start = time.clock()
base = [i for i in range(1000000)]
tmplist = [i for i in range(1000000)]
 
tmp = []
for i in base:
  tmp = tmplist[i]
 
end = time.clock()
print(end - start)
 
 
 
0.28361699999999956
In [13]:
 
import time
start = time.clock()
base = tuple([i for i in range(1000000)])
tmplist = tuple([i for i in range(1000000)])
 
 
tmp = 0
for i in base:
  tmp = tmplist[i]
 
end = time.clock()
print(end - start)
 
 
 
0.2708940000000002

 う~~んリストからtupleの変換までのトータルコストで考えると

ほとんど変わらない。。。。

使い回しのタプルならいいけどわざわざ変換してまで

tuple使うのはどうなんだろう。。。ちょっと考慮する必要があるなぁ

 うーん使いドコロが微妙

 どういう使い方がいいんだろう

 誰か教えて下さい。。。m(_ _)m