iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0

想像一下在日常生活中,我們有許多東西需要存放,這時你可能會找一個空間把這些東西放入,好的方式是將東西分門別類存放,但我想...大多數的人都是看到這個地方還有空間就塞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']

整理了一些常用方法提供參考
新增:

  • append():在尾巴加入元素
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruites.append("peach")
print(fruites) # ['banana', 'orange', 'apple', 'tomato', 'mango', 'lemon', 'peach']
  • extend():在尾巴加入多個元素
    一次性加入多個元素或是將某列表內容加入到另一列表內時可使用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()
    增加至指定位置可使用insert()方法。
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.insert(2, "peach")
print(fruits) # ['banana', 'orange', 'peach', 'apple', 'tomato', 'mango', 'lemon']

刪除:

  • pop()
    移除串列中最後一個元素,該方法會回傳移除的那個元素,也可以根據索引值移除並回傳該索引值位置,如果要刪除的索引超過範圍則會回傳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
  • remove()
    移除元素。
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.remove("mango")
print(fruits) # ['banana', 'orange', 'apple', 'tomato', 'lemon']
  • clear()
    清空串列。
    仔細看可以發現使用clear()後還是會存在一個空串列,所以才會稱為清空而不是刪除,如果真的需要刪除那麼可以使用del來刪除指定的元素,請謹慎使用避免開發過程涼掉。
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.clear()
print(fruits) # []

計算:

  • index()
    查詢索引值。
    如果查詢一個不存在的元素則會回傳錯誤訊息:ValueError: 'abc' is not in list
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
new_fruits = fruits.index("tomato")
print(new_fruits) # 3
  • count()
    計算串列中指定元素的數量。
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
print(fruits.count("tomato")) # 1

排序:

  • sort()
    會改變原串列的內容,預設為升冪。
    sort()提供兩個參數,keyreversekey表示提供函式排序的依據,reverse預設是False(升冪),如果是True表示降冪。
fruits = ["banana", "orange", "apple", "tomato", "mango", "lemon"]
fruits.sort()
print(fruits) # ['apple', 'banana', 'lemon', 'mango', 'orange', 'tomato']
  • sorted()
    不會改變原串列內容,保存資料的完整性且不輕易被修改,同時又整理過。
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']
  • reverse()
    反轉串列的內容,會直接修改原串列內容,如果不想要更動原串列內容可以使用先前所提到的切片方式。
nums = [1, 2, 3, 4]
nums.reverse()
print(nums) # [4, 3, 2, 1]

複製:

  • copy()
    複製出一個全新的串列。
    copy()方法為淺拷貝,有淺當然有深,之後再找時間說明XD
a = [1, 2, 3]
b = a.copy()
b.append(26)
print(a) # [1, 2, 3]
print(b) # [1, 2, 3, 26]

做個小總結,列表是『有序 + 可變』的,學會以上的小方法大概可以完成日常任務的八成需求!?

那麼今天就介紹到這,明天見ㄅㄅ!


上一篇
筆記Day8:補充-字串 String
下一篇
筆記Day10:集合 Set
系列文
Python 學習筆記10
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言