1.資料型態:list與tuple#

群集資料型別:list#

list是一種集合了多個元素且元素之間具有順序的資料型態。

list中的元素可以放任何物件,bool, int, float, str都可以,甚至是 list自己本身,所以當然就還包括dict, tuple, set……其他任何物件。

建立list

# 以下兩種皆可

a_list = ['a','b','c']
a_list = list(['a','b','c'])
# list中的list

a_list = ['a', 'b', 'c', [1, 2, 3]]

list slice#

切片slice

需注意切片是從0開始。

a_list = ['a','b','c']

print(a_list[0])
print(a_list[-1])
print(a_list[1:])
print(a_list[0:1])
print(a_list[::-1])
a
c
['b', 'c']
['a']
['c', 'b', 'a']

超過index會報錯:IndexError。

a_list = ['a','b','c']

print(a_list[3])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[4], line 3
      1 a_list = ['a','b','c']
----> 3 print(a_list[3])

IndexError: list index out of range
a_list = ['a', 'b', 'c', [1, 2, 3]]

print(a_list[3][2])
3

利用切片為list賦值

a_list = ['a','b','c']

a_list[0] = 1
print(a_list)

a_list[1:] = 2, 3 # (2, 3) or [2, 3] or {2, 3} 皆可
print(a_list)
[1, 'b', 'c']
[1, 2, 3]

list index#

取得list中元素的位置.index()

如果元素不存在也會報錯:IndexError。

a_list = ['a','b','c']

print(a_list.index('a'))

print(a_list.index('d'))
0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[7], line 5
      1 a_list = ['a','b','c']
      3 print(a_list.index('a'))
----> 5 print(a_list.index('d'))

ValueError: 'd' is not in list

list 元素操作#

在尾端加入元素.append()

a_list = ['a','b','c']

a_list.append('d')  # 直接會改變list的內容 且不回傳結果
print(a_list)
['a', 'b', 'c', 'd']

刪除list中的元素.remove()

a_list = ['a','b','c', 'd', 'e']

a_list.remove('a') # 直接會改變list的內容 且不回傳結果
print(a_list)
['b', 'c', 'd', 'e']
# 另外一種方式
a_list.pop() # 會取出最後一個元素回傳並從list中刪除
print(a_list)

a_list.pop(0) # ()中可以指定要pop的位置
print(a_list)
['b', 'c', 'd']
['c', 'd']
# 還有另外一種方式
del a_list[1] # del 事實上並非是list方法,而是python內建方法,會將物件和名稱分開。

print(a_list)
['c']

檢查元素是否存在(in)

a_list = ['a','b','c']
'a' in a_list
True
'd' in a_list
False

list中元素的排序

a_list = ['b','d','a','c','e']

a_list.sort() # 直接會改變list的內容 且不回傳結果
print(a_list)

a_list.sort(reverse=True) # 降冪排列
print(a_list)
['a', 'b', 'c', 'd', 'e']
['e', 'd', 'c', 'b', 'a']
a_list = ['b','d','a','c','e']

print(sorted(a_list)) # 只會回傳資料的「副本」

print(a_list) # 不影響a_list的內容。
['a', 'b', 'c', 'd', 'e']
['b', 'd', 'a', 'c', 'e']

list 操作#

取得list的長度

a_list = [1,2,3]

len(a_list)
3

list的加法與乘法運算

a_list = ['a','b','c']
d_list = ['d','e','f']

print(a_list + d_list)
['a', 'b', 'c', 'd', 'e', 'f']
# 補充:用append的話,argument會被當作元素加到list最尾端

a_list.append(d_list) # 直接會改變list的內容且不回傳結果
print(a_list)
['a', 'b', 'c', ['d', 'e', 'f']]
a_list = ['a','b','c']

print(a_list * 2)
['a', 'b', 'c', 'a', 'b', 'c']

list的其他方法#

.reverse() 反序一個list

a_list = ['a','b','c']

a_list.reverse() # 直接會改變list的順序 且不回傳結果
print(a_list)
['c', 'b', 'a']

.extend() 一個list延展至另一個list

a_list = ['a','b','c']
d_list = ['d','e','f']

a_list.extend(d_list) # 直接會改變list的內容 且不回傳結果

.clear() 清除一個list的內容

a_list = ['a','b','c']

a_list.clear() # 直接會改變list的內容 且不回傳結果
print(a_list)
[]

.count() 計算指定元素的數量

a_list = ['a','b','c','a']

a_list.count('a')
2

群集資料型別:tuple#

tuple也是一種集合了多個元素且元素之間具有順序的資料型態。 但跟list有一些使用上的差別。

建立tuple

a_tuple = (1, 2, 3)
a_tuple = tuple([1,2,3])
a_tuple = 1, 2, 3

tuple slice#

切片(slice)

a_tuple = (1, 2, 3)

print(a_tuple[0])
print(a_tuple[1:])
print(a_tuple[0:1])
1
(2, 3)
(1,)

tuple 元素操作#

tuple unpacking

以下這種做法可以拆解tuple,把元素賦值到不同變數中:

name = ('Frieren', 'Fern', 'Stark')

a, b, c = name

print(a)
print(b)
print(c)
Frieren
Fern
Stark

檢查元素是否存在(in)

a_tuple = (1,2,3)

4 in a_tuple
False

tuple 操作#

取得tuple的長度

a_tuple = (1,2,3)

len(a_tuple)
3

tuple的加法與乘法運算

a_tuple = (1,2,3)
b_tuple = (4,5,6)

print(a_tuple + b_tuple)
print(a_tuple * 2)
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 1, 2, 3)

tuple與list的差異#

tuple不能切片後賦值, 會報錯:TypeError。

# list 可以切片後賦值
a_list = [1,2,3]
a_list[0] = 'a'
print(a_list)
['a', 2, 3]
# tuple不行
a_tuple = (1,2,3)

a_tuple[0] = 'a'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[32], line 4
      1 # tuple不行
      2 a_tuple = (1,2,3)
----> 4 a_tuple[0] = 'a'

TypeError: 'tuple' object does not support item assignment

tuple不能新增元素,沒有類似.append()或.insert()方法,要增加元素只能新建一個tuple。

# 不能新增元素,沒有append()方法,要增加元素只能新建一個tuple
a_tuple = (1,2,3)

print(id(a_tuple)) # 物件的唯一識別碼

a_tuple = a_tuple + (4,)
print(id(a_tuple))

a_list = [1,2,3]
print(id(a_list))

a_list.append(4)
print(id(a_list))
4402951616
4402909792
4402898240
4402898240

tuple沒有sort方法。

# 沒有sort方法,要排序tuple中的元素的話,可以用sorted內建函數,但回傳的是一個list
a_tuple = (4,5,3,1,2)

print(sorted(a_tuple))
[1, 2, 3, 4, 5]