想像一下在日常生活中,我們有許多東西需要存放,這時你可能會找一個空間把這些東西放入,好的方式是將東西分門別類存放,但我想...大多數的人都是看到這個地方還有空間就塞XD
必需用到淋漓盡致!
今天要介紹的是列表 List,列表可以用來存放有順序性、可變的資料,想放數字
、字串
、布林值
、None
都可以。
上述提到列表是可變的,意思是在建立後可以新增、修改、刪除元素。
lst = ["hello", 1, None]
print(lst) # ['hello', 1, None]
lst = ["apple", 1, None]
lst[1] = 0
print(lst) # ['apple', 0, None]
Python 中的列表在實際應用上儲存空間取決於記憶體,但我想基本上是用不完的!?
要知道列表長度就需要使用到先前所提到的len()
這個內建方法啦!
這樣是不是就記得一個方法了XD
lst = ["apple", 1, None]
print(len(lst)) # 3
這時候也需要使用到前幾天所提到的索引值
lst = ["apple", 1, None]
print(lst[1]) # 1
順便複習一下索引 index
和切片 slice
吧~
再說一次!索引起始值是從0
開始
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
print(fruits[1]) # orange
print(fruits[-1]) # lemon
print(fruits[0:3]) # ['banana', 'orange', 'apple']
print(fruits[:2]) # ['banana', 'orange']
print(fruits[2:]) # ['apple', 'tomato', 'mango', 'lemon']
整理了一些常用方法提供參考
新增:
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruites.append("peach")
print(fruites) # ['banana', 'orange', 'apple', 'tomato', 'mango', 'lemon', 'peach']
extend()
方法,使用extend()
方法時會將原先的內容改變。fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
other_fruits = ["peach", "peanut", "coconut"]
fruits.extend(other_fruits)
print(fruits) # ['banana', 'orange', 'apple', 'tomato', 'mango', 'lemon', 'peach', 'peanut', 'coconut']
insert()
方法。fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.insert(2, "peach")
print(fruits) # ['banana', 'orange', 'peach', 'apple', 'tomato', 'mango', 'lemon']
刪除:
IndexError: pop index out of range
。pop()
和remove()
區別則是,pop()
方法是根據索引進行刪除,remove()
方法是根據值進行刪除。fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
# 不傳入參數:移除最後一個元素
last_fruits = fruits.pop()
# 傳入索引值:移除指定位置元素
second_fruits = fruits.pop(2)
# 超出索引範圍會報錯
tail_fruits = fruits.pop(100)
print(last_fruits) # lemon
print(second_fruits) # apple
print(tail_fruits) # IndexError: pop index out of range
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.remove("mango")
print(fruits) # ['banana', 'orange', 'apple', 'tomato', 'lemon']
clear()
後還是會存在一個空串列,所以才會稱為清空
而不是刪除,如果真的需要刪除那麼可以使用del
來刪除指定的元素,請謹慎使用避免開發過程涼掉。fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.clear()
print(fruits) # []
計算:
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
new_fruits = fruits.index("tomato")
print(new_fruits) # 3
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
print(fruits.count("tomato")) # 1
排序:
sort()
提供兩個參數,key
和reverse
,key
表示提供函式排序的依據,reverse
預設是False
(升冪),如果是True
表示降冪。fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.sort()
print(fruits) # ['apple', 'banana', 'lemon', 'mango', 'orange', 'tomato']
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
new_fruits = sorted(fruits)
print(fruits) # ['banana', 'orange', 'apple', 'tomato', 'mango', 'lemon']
print(new_fruits) # ['apple', 'banana', 'lemon', 'mango', 'orange', 'tomato']
nums = [1, 2, 3, 4]
nums.reverse()
print(nums) # [4, 3, 2, 1]
複製:
copy()
方法為淺拷貝,有淺當然有深,之後再找時間說明XDa = [1, 2, 3]
b = a.copy()
b.append(26)
print(a) # [1, 2, 3]
print(b) # [1, 2, 3, 26]
做個小總結,列表是『有序 + 可變』的,學會以上的小方法大概可以完成日常任務的八成需求!?
那麼今天就介紹到這,明天見ㄅㄅ!