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