清單簡介
Python 有許多內建類型,例如字串和整數。 Python 具有儲存值集合的類型:清單。
建立清單
您可以透過將值序列指派給變數來建立清單。 每個值都會以逗號分隔,並以方括弧 ([]
) 括住。 下列範例會將所有行星的清單儲存於 planets
變數中:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
依索引存取清單項目
您可以在變數名稱之後,將「索引」放在方括弧 []
內,以存取清單中的任何項目。 索引從 0 開始,因此,在下列程式碼中,planets[0]
是 planets
清單中的第一個項目:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
print("The first planet is", planets[0])
print("The second planet is", planets[1])
print("The third planet is", planets[2])
The first planet is Mercury
The second planet is Venus
The third planet is Earth
注意
因為所有索引都從 0 開始,所以,[1]
為第二個項目、[2]
為第三個項目,依此類推。
您也可以使用索引來修改清單中的值。 您可以透過指派新值來執行此動作,方式則與指派變數值的方式大致相同。 例如,您可以變更清單中 Mars 的名稱,以使用其暱稱:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
planets[3] = "Red Planet"
print("Mars is also known as", planets[3])
輸出:Mars is also known as Red Planet
判斷清單長度
若要取得清單長度,請使用 len()
內建函式。 下列程式碼會建立新變數 number_of_planets
。 程式碼會為該變數指派 planets
清單中的項目數 (8)。
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
number_of_planets = len(planets)
print("There are", number_of_planets, "planets in the solar system.")
輸出:There are 8 planets in the solar system
將值新增至清單
Python 中的清單是動態的:您可以在建立項目之後新增和移除它們。 若要將項目新增至清單,請使用 .append(value)
方法。
例如,下列程式碼會將 "Pluto"
字串新增至 planets
清單的結尾:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
planets.append("Pluto")
number_of_planets = len(planets)
print("There are actually", number_of_planets, "planets in the solar system.")
輸出:There are actually 9 planets in the solar system.
從清單中移除值
您可以在清單變數上呼叫 .pop()
方法,以移除清單中的最後一個項目:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
planets.pop() # Goodbye, Pluto
number_of_planets = len(planets)
print("No, there are definitely", number_of_planets, "planets in the solar system.")
使用負數索引
您已了解如何使用索引來擷取清單中的個別項目:
print("The first planet is", planets[0])
輸出:The first planet is Mercury
索引從零開始並增加。 負數索引會從清單結尾開始反向運作。
在下列範例中,索引 -1
會傳回清單中的最後一個項目。 索引 -2
會傳回第二個到最後一個。
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
print("The last planet is", planets[-1])
print("The penultimate planet is", planets[-2])
The last planet is Neptune
The penultimate planet is Uranus
如果您想要傳回第三個到最後一個,您會使用索引 -3
(依此類推)。
在清單中尋找值
若要判斷清單中儲存值的位置,您可以使用清單的 index
方法。 此方法會搜尋值,並傳回該項目在清單中的索引。 如果找不到相符項目,則會傳回 -1
。
下列範例示範如何使用 "Jupiter"
作為索引值:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
jupiter_index = planets.index("Jupiter")
print("Jupiter is the", jupiter_index + 1, "planet from the sun")
輸出:Jupiter is the 5 planet from the sun
注意
因為索引編製從 0 開始,所以您必須增加 1 才能顯示適當的數字。
以下是另一個:
planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
mercury_index = planets.index("Mercury")
print("Mercury is the", mercury_index + 1, "planet from the sun")
輸出:Mercury is the 1 planet from the sun