List 串列
串列是任何類型元素的序列,並且是可變的。用於儲存項目集合,它們可以包含任何型別的資料,並以方(中)括號表示。
a = [1, 2, 3, 4, 5]
b = ['mango', 'pineapple', 'orange']
在 Python 中,List 和 String 非常相似。它們都是資料序列的範例。序列有類似的屬性,例如:
- 可以使用
for
迴圈迭代序列 - 支援索引 indexing
- 使用
len()
函數找出序列的長度 - 使用加號運算符
+
來串連 - 使用
in
關鍵字來檢查序列是否包含一個值
List 與 String 的差異是,String 內容是不可變的 (immutable);List 內容可以變動 (mutable)。
List methods
list[index] = x
變更指定 index 的元素
list.append()
numbers = [1, 2, 3, 4]
numbers.append(5)
print(numbers)
# output: [1, 2, 3, 4, 5]
list.insert()
animals = ["cat", "dog", "fish"]
animals.insert(1, "monkey")
print(animals)
# output: ["cat", "monkey", "dog", "fish"]
animals = ["cat", "dog", "fish"]
animals.insert(200, "monkey")
print(animals)
# output: ["cat", "dog", "fish", "monkey"]
list.extend()
things = ["John", 42, True]
other_things = [0.0, False]
things.append(other_things)
print(things)
# output: ["John", 42, True, [0.0, False]]
things = ["John", 42, True]
other_things = [0.0, False]
things.extend(other_things)
print(things)
# output: ["John", 42, True, 0.0, False]
list.remove()
Note: If there are two of the same element in a list, the .remove() method only removes the first instance of that element and not all occurrences.
booleans = [True, False, True, True, False]
booleans.remove(False) # Removes the first False value
print(booleans)
# output: [True, True, True, False]
booleans.remove(False) # Removes the other False value
print(booleans)
# output: [True, True, True]
booleans.remove(False) # ValueError! No more False values to remove
list.pop()
fruits = ["apple", "orange", "banana", "peach"]
last_fruit = fruits.pop() # takes the last element
print(last_fruit)
# output: "peach"
second_fruit = fruits.pop(1) # takes the second element ( = index 1)
print(second_fruit)
# output: "orange"
print(fruits) # only fruits that have not been "popped"
# are still in the list
# output: ["apple", "banana"]
list.clear()
decimals = [0.1, 0.2, 0.3, 0.4, 0.5]
decimals.clear() # remove all values!
print(decimals)
# output: []
list.count()
grades = [7.8, 10.0, 7.9, 9.5, 10.0, 6.5, 9.8, 10.0]
n = grades.count(10.0)
print(n)
# output: 3
list.index()
Note: it only returns the index of the first occurrence of a list item.
friends = ["John", "James", "Jessica", "Jack"]
position = friends.index("Jessica")
print(position)
# output: 2
list.sort() and list.reverse()
values = [10, 4, -2, 1, 5]
values.reverse()
print(values) # list is reversed
# output: [5, 1, -2, 4, 10]
values.sort()
print(values) # list is sorted
# output: [-2, 1, 4, 5, 10]
values = [10, 4, -2, 1, 5]
values.sort(reverse=True)
print(values) # list is sorted in reverse order
# output: [10, 5, 4, 1, -2]
list.copy()
values_01 = [1, 2, 3, 4]
values_02 = values_01 # not an actual copy: same list object!
values_02.append(5) # we modify the "values_02" list...
print(values_01) # ... but changes appear also in "values_01"
# because they reference the same list!
# output: [1, 2, 3, 4, 5]
values_01 = [1, 2, 3, 4]
values_02 = values_01.copy() # create an independent copy!
values_02.append(5) # we modify the "values_02" list...
print(values_01) # ... and changes DO NOT appear in "values_01"
# because it is a copy!
# output: [1, 2, 3, 4]
List functions
sorted()
無法用在不同 data typesorted()
不會更動變數原始內容min()
串列裡最小值max()
串列裡最大值map(function, iterable)
Python - map() functionzip(*iterables)
Python zip() function
time_list = [12, 2, 32, 19, 57, 22, 14]
print(sorted(time_list))
print(time_list)
time_list = [12, 2, 32, 19, 57, 22, 14]
print(min(time_list))
print(max(time_list))
Extracting from a list
# A element from a list
username_list = ["elarson", "fgarcia", "tshah", "sgilmore"]
print(username_list[2])
# one-liner
print(["elarson", "fgarcia", "tshah", "sgilmore"][2])
# A slice from a list
username_list = ["elarson", "fgarcia", "tshah", "sgilmore"]
print(username_list[0:2])
List with Loop
animals = ["Lion", "Zebra", "Dolphin", "Monkey"]
chars = 0
for animal in animals:
chars += len(animal)
print("Total characters: {}, Average length: {}".format(chars, chars/len(animals)))
# Output: Total characters: 22, Average length: 5.5
enumerate()
函式會為串列中的每個元素回傳一個 tuple(元組)。元組中的第一個值是該元素在序列中的索引。元組中的第二個值是序列中的元素
winners = ["Ashley", "Dylan", "Reese"]
for index, person in enumerate(winners):
print("{} - {}".format(index + 1, person))
# Output:
#1 - Ashley
#2 - Dylan
#3 - Reese
Output by line + 2 "\n"
IDs = ["001","002","003","004"]
print("\n\n".join([id for id in IDs]))
For + If
mylist = [1, 4, 7, 8, 20]
newlist = [x for x in mylist if x % 2 == 0]
print(newlist)
Range()
mylist = ["a", "b", "c", "d", "e", "f", "g"]
for x in range(2, len(mylist) - 1):
print(mylist[x])
List comprehensions
串列綜合運算。一個 list comprehension 的組成,是在一對方括號內,放入一個 expression(運算式)、一個 for 子句、再接著零個或多個 for 或 if 子句。結果會是一個新的 list,內容是在後面的 for 和 if 子句情境下,對前面運算式求值的結果
for loop vs. list comprehensions
# For Loop
multiples = []
for x in range(1,11):
multiples.append(x*7)
print(multiples)
# List comprehensions
multiples = [x*7 for x in range(1,11)]
print(multiples)
# Output [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
Examples: Basic
languages = ["Python", "Perl", "Ruby", "Go", "Java", "C"]
lengths = [len(language) for language in languages]
print(lengths)
# Output [6, 4, 4, 2, 4, 1]
z = [x for x in range(0,101) if x % 3 == 0]
print(z)
# Output [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
Examples: 建立多組 Tuple 的 List
# Create a list of tuples where each tuple contains the numbers 1, 2, and 3.
numbers = [(1, 2, 3) for _ in range(5)]
# numbers: [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]
Examples: 函式回傳 List
def squares(start, end):
return [ n * n for n in range(start, end+1) ]
print(squares(2, 3)) # Should print [4, 9]
print(squares(1, 5)) # Should print [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should print [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
其他類型
Dictionary
- 符號用大括號
- key:value 格式
{ 1: "East",
2: "West",
3: "North",
4: "South" }
Set
- 符號用大括號
- 內容必須是唯一值,不可重複
{"jlanksy", "drosas", "nmason"}