list 串列變數

Python 基礎: list 串列變數

一般 list 串列

list_1 = [1, 2, 3, 4, 5]
print("list_1: ", list_1)
print("list_1[3]: ", list_1[3])

# list:  [1, 2, 3, 4, 5]
# list[3]:  4
list_1 = [1, 'KJ', True]
print("list_1: ", list_1)

# list_1:  [1, 'KJ', True]

最後一個元素

list_1 = [1, 2, 3, 4, 5]
print("list_1: ", list_1)
print("list_1[-1]: ", list_1[-1])

# list:  [1, 2, 3, 4, 5]
# list[-1]:  5

重複串列

list_1 = [1, 2, 3]
list_2 = list_1 * 2
print("list_1: ", list_1)
print("list_2: ", list_2)

# list_1:  [1, 2, 3]
# list_2:  [1, 2, 3, 1, 2, 3]

取得串列中間元素

list_1 = [1, 2, 3, 4, 5, 6, 7]
list_2 = list_1[2:5]
print("list_1: ", list_1)
print("list_2: ", list_2)

# list_1:  [1, 2, 3, 4, 5, 6, 7]
# list_2:  [3, 4, 5]
list_1 = [1, 2, 3, 4, 5]
# 取出第一個元素
list_2 = list_1[:1]
print("list_1: ", list_1)
print("list_2: ", list_2)

# list_1:  [1, 2, 3, 4, 5]
# list_2:  [1]
list_1 = [1, 2, 3, 4, 5]
# 取出第一個元素後面的元素
list_2 = list_1[1:]
print("list_1: ", list_1)
print("list_2: ", list_2)

# list_1:  [1, 2, 3, 4, 5]
# list_2:  [2, 3, 4, 5]

刪除串列中間元素

list_1 = [1, 2, 3, 4, 5, 6, 7]
del list_1[2:5]
print("list_1: ", list_1)

# list_1:  [1, 2, 6, 7]

計算元素

函式 說明
len 串列長度
max 串列最大值
min 串列最小值
list_1 = [1, 2, 3, 4, 5, 6, 7]
# 數量
list_1_length = len(list_1)
# 最小值
list_1_min = min(list_1)
# 最大值
list_1_max = max(list_1)

print("list_1: ", list_1)
print("list_1_length: ", list_1_length)
print("list_1_min: ", list_1_min)
print("list_1_max: ", list_1_max)

# list_1:  [1, 2, 3, 4, 5, 6, 7]
# list_1_length:  7
# list_1_min:  1
# list_1_max:  7
函式 說明
list.index 元素第一次出現位置
list.count 元素出現數量
list_1 = [1, 2, 3, 4, 5, 1, 2, 3, 2, 3]
# 元素 3 第一次出現位置
list_1_element3_first_index = list_1.index(3)
# 元素 3 出現次數
list_1_element3_nums = list_1.count(3)

print("list_1: ", list_1)
print("list_1_element3_first_index: ", list_1_element3_first_index)
print("list_1_element3_nums: ", list_1_element3_nums)

# list_1:  [1, 2, 3, 4, 5, 1, 2, 3, 2, 3]
# list_1_element3_first_index:  2
# list_1_element3_nums:  3

加入元素

list.append() 將元素加在最後方

list_1 = [1, 2, 3, 4, 5]
# 將元素加在最後方
list_1.append(55)
print("list_1: ", list_1)

# list_1:  [1, 2, 3, 4, 5, 55]

list.extend() 將其他串列加在最後方

list_1 = [1, 2, 3, 4, 5]
# 將其他串列加在最後方
list_1.extend([55, 66])
print("list_1: ", list_1)

# list_1:  [1, 2, 3, 4, 5, 55, 66]

list.insert() 將元素加在指定位置

list_1 = [1, 2, 3, 4, 5]
# 將元素加在指定位置
list_1.insert(3, 55)
print("list_1: ", list_1)

# list_1:  [1, 2, 3, 55, 4, 5]
list_1 = [1, 2, 3, 4, 5]
# 將元素加在指定位置
list_1.insert(3, [55, 66])
print("list_1: ", list_1)
list_1:  [1, 2, 3, [55, 66], 4, 5]

# list_1:  [1, 2, 3, [55, 66], 4, 5]

list.pop() 取出最後一個元素

list_1 = [1, 2, 3, 4, 5]
# 取出最後一個元素
last_element = list_1.pop()
print("list_1: ", list_1)
print("last_element: ", last_element)

# list_1:  [1, 2, 3, 4]
# last_element:  5

list.pop() 取出並移除指定元素

list_1 = [1, 2, 3, 4, 5]
# 取出第一個元素
last_element = list_1.pop(0)
print("list_1: ", list_1)
print("last_element: ", last_element)

# list_1:  [2, 3, 4, 5]
# last_element:  1
list_1 = [1, 2, 3, 4, 5]
# 取出第一個元素
last_element = list_1.pop(1)
print("list_1: ", list_1)
print("last_element: ", last_element)

# list_1:  [1, 3, 4, 5]
# last_element:  2

list.remove() 移除第一次出現的元素

list_1 = [1, 2, 3, 4, 5, 2, 3]
print("list_1: ", list_1)
# 移除第一次出現的元素「3」
list_1.remove(3)
print("list_1: ", list_1)

# list_1:  [1, 2, 3, 4, 5, 2, 3]
# list_1:  [1, 2, 4, 5, 2, 3]

list.reverse() 反轉串列

list_1 = [1, 2, 3, 4, 5]
# 反轉串列
list_1.reverse()
print("list_1: ", list_1)

# list_1:  [5, 4, 3, 2, 1]

list.sort() 排序串列

從小到大

list_1 = [7, 6, 1, 4, 3, 2, 5]
# 排序串列
list_1.sort()
print("list_1: ", list_1)

# list_1:  [1, 2, 3, 4, 5, 6, 7]

從大到小

list_1 = [7, 6, 1, 4, 3, 2, 5]
# 元素排序
list_1.sort(reverse=1)
print("list_1: ", list_1)

# list_1:  [7, 6, 5, 4, 3, 2, 1]

enumerate() 列舉串列 for in list

要讓 list 能夠 for in 需要先 enumerate list 才可以 for in

list_1 = [1, 2, 3]
print("list_1: ", list_1)
enumerate_list_1 = enumerate(list_1)
print("enumerate_list_1: ", enumerate_list_1)

for key, value in enumerate_list_1:
    print('key: ', key)
    print('value: ', value)

# list_1:  [1, 2, 3]
# enumerate_list_1:  <enumerate object at 0x10634b980>
# key:  0
# value:  1
# key:  1
# value:  2
# key:  2
# value:  3

參考資料