iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 11
0

概觀(Overview)

tuple是sequence type的一種,有不可異動(immutable)的特性,通常用來存放異質性(heterogeneous的資料。

可以indexedunpacking的方式取出tuple的元素。

宣告

  • 括號(parentheses)
    • 0或1個元素
  • tuple()

empty tuple,但有甚麼用途呢?

# empty tuple
demo = ()
print(demo)
print( "type of demo: {}".format( type(demo) ) )
print( "len of demo: {}".format( len(demo) ) )
print( "id of demo: {}".format( id(demo) ) )

# ()
# type of demo: <class 'tuple'>
# len of demo: 0
# id of demo: 140183861809224
# Type Error: 'tuple' object does not support item assignment


try:
    demo[0] = 1
except TypeError as e:
    print( "Type Error: {0}".format(e) )

demo2 = tuple()
print(demo2)
print( "type of demo2: {}".format( type(demo2) ) )
print( "len of demo2: {}".format( len(demo2) ) )
print( "id of demo2: {}".format( id(demo2) ) )

# ()
# type of demo2: <class 'tuple'>
# len of demo2: 0
# id of demo2: 140183861809224

只有一個元素的tuple

# single element tuple
demo3 = 1,
print(demo3)
print( "type of demo3: {}".format( type(demo3) ) )
print( "len of demo3: {}".format( len(demo3) ) )
print( "id of demo3: {}".format( id(demo3) ) )

# (1,)
# type of demo3: <class 'tuple'>
# len of demo3: 1
# id of demo3: 140183861180232


demo4 = (2,)
print(demo4)
print( "type of demo4: {}".format( type(demo4) ) )
print( "len of demo4: {}".format( len(demo4) ) )
print( "id of demo4: {}".format( id(demo4) ) )

# (2,)
# type of demo4: <class 'tuple'>
# len of demo4: 1
# id of demo4: 140183860646744

多個元素和異質性元素

# multiple elements
demo5 = 1, 3, 5, 7, 9
print(demo5)
print( "type of demo5: {}".format( type(demo5) ) )

# (1, 3, 5, 7, 9)
# type of demo5: <class 'tuple'>


demo6 = (2, 4, 6, 8, 10)
print(demo6)
print( "type of demo6: {}".format( type(demo6) ) )

# (2, 4, 6, 8, 10)
# type of demo6: <class 'tuple'>


# with mutable object
squre = [x**2 for x in range(10)]
demo7 = ('squre result', squre)
print(demo7)
print( "type of demo7: {}".format( type(demo7) ) )


# ('squre result', [0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
# type of demo7: <class 'tuple'>

操作

操作層面分為取出資料、如何走訪

取出資料

可以取出tuple中的某一個資料和部分資料,分別為indexed和sliced或者利用unpacking

demo = ('name', 'age', 'height', 'weight')

# indexed
try:
    print(demo[0])
    print(demo[3])
    print(demo[4])
except IndexError as e:
    print( "Index Error: {0}".format(e) )

# name
# weight
# Index Error: tuple index out of range


# unpacking
name, age, height, weight = demo
print( "{}, {}, {}, {}".format(name, age, height, weight ) )

# name, age, height, weight


# sliced
s1 = demo[:]
print( "sliced 1:{}".format(s1) )

s2 = demo[2:]
print( "sliced 2:{}".format(s2) )

s3 = demo[2:5]
print( "sliced 3:{}".format(s3) )

s4 = demo[:5]
print( "sliced 4:{}".format(s4) )

s5 = demo[-3:]
print( "sliced 5:{}".format(s5) )

s6 = demo[::2]
print( "sliced 6:{}".format(s6) )

s7 = demo[1::2]
print( "sliced 7:{}".format(s7) )

# sliced 1:('name', 'age', 'height', 'weight')
# sliced 1:('name', 'age', 'height', 'weight')
# sliced 2:('height', 'weight')
# sliced 3:('height', 'weight')
# sliced 4:('name', 'age', 'height', 'weight')
# sliced 5:('age', 'height', 'weight')
# sliced 6:('name', 'height')
# sliced 7:('age', 'weight')

如何走訪

和list相同,使用for加上in或是while走訪tuple

# iteration
for ele in demo:
    print(ele)

# name
# age
# height
# weight


idx = 0
while idx < len(demo):  
    print(demo[idx])
    idx += 1

# name
# age
# height
# weight

參考


上一篇
Day13-Dictionary-常用方式
下一篇
Day15-Set
系列文
if len(learning.python) == 30:31

尚未有邦友留言

立即登入留言