列表list是由方括號括起來的可修改、可重複的數據集
創建列表
myList = ["banana", "cherry", "apple"]
print(myList)
['banana', 'cherry', 'apple']
創建空列表
myList2 = list() #創建一個空列表list
# myList2 = []
print(myList2)
[]
列表允許不同的類型數據
myList = ['apple', True, 19]
print(myList)
['apple', True, 19]
列表允許數據重複
myList = ['apple', True, 19, 'apple']
print(myList)
['apple', True, 19, 'apple']
使用索引號訪問列表種的數據
myList = ['banana', 'cherry', 'apple']
print(myList[1]
cherry
索引號由0開始,最大值為len(myList)-1,超出範圍會拋出IndexError異常
myList = ['banana', 'cherry', 'apple']
print(myList[3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
也可以使用負數索引,索引-1表示最後一個元素,-2表示倒數第二個元素
myList = ['banana', 'cherry', 'apple']
print(myList[-1])
apple
迭代遍歷列表種的元素
myList = ['banana', 'cherry', 'apple']
for item in myList:
print(item)
banana
cherry
apple
查詢列表種是否有某一個元素
myList = ['banana', 'cherry', 'apple']
if 'apple' in myList:
print('Yes')
else:
print('No')
Yes
列表的長度由len()給出
myList = ['banana', 'cherry', 'apple']
print(len(myList))
3
給列表添加元素
myList = ['banana', 'cherry', 'apple']
myList.append("lemon") #在末尾添加
print(myList)
['banana', 'cherry', 'apple', 'lemon']
添加元素到列表指定位置
myList = ['banana', 'cherry', 'apple']
myList.insert(1, 'lemon') #在索引號1插入
print(myList)
['banana', 'lemon', 'cherry', 'apple']
移除末端元素
myList = ['banana', 'cherry', 'apple']
item = myList.pop() #移除末端元素,將其返回
print(item)
print(myList)
apple
myList = ['banana', 'cherry']
移除指定元素
myList = ['banana', 'cherry', 'apple']
myList.remove("cherry") #移除cherry,不將其返回
print(myList)
myList = ['banana', 'apple']
列表不存在指定元素,則移除時會拋出ValueError異常
myList = ['banana', 'cherry', 'apple']
myList.remove('lemon')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
移除所有元素
myList = ['banana', 'cherry', 'apple']
myList.clear()
print(myList)
[]
反轉元素排列
myList = ['banana', 'cherry', 'apple']
myList.reverse()
print(myList)
['apple', 'cherry', 'banana']
排序元素
myList = [4, 2, 1, -1, -3, 5, 10]
myList.sort()
print(myList)
[-3, -1, 1, 2, 4, 5, 10]
用同一值初始化多個元素
myList = [0]*10
print(myList)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
連接兩個列表
myList = [0] * 5
myList = [1, 2, 3, 4, 5]
newList = myList + myList2
print(newList)
[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
給列表切片
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = myList[1:5] # 切片時指定start:stop,新切片範圍[start, stop),包含start指向的元素,但不包含stop指向的元素
print(myList2)
[2, 3, 4, 5]
如果不指定start,則從索引0開始
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = myList[:5]
print(myList2)
[6, 7, 8, 9]
[1, 2, 3, 4, 5]
不指定stop,則直至列表末
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = myList[5:]
print(myList2)
[6, 7, 8, 9]
還可以指定索引增量
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = myList[::2] #提取偶數索引號裏面的元素構成新切片
print(myList2)
[1, 3, 5, 7, 9]
使用-1為指定索引增量,得到是原列表的反序列表
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = myList[::-1]
print(myList2)
[9, 8, 7, 6, 5, 4, 3, 2, 1]
將一個列表變量賦值給另外一個列表變量,實際上它們同時指向同一個列表內存空間
orig = ['banana', 'cherry', 'apple']
cpy = orig
cpy.append('lemon')
print(orig)
print(cpy)
['banana', 'cherry', 'apple', 'lemon']
['banana', 'cherry', 'apple', 'lemon']
要拷貝列表,則需要列表的copy()方法
orig = ['banana', 'cherry', 'apple']
cpy = orig.copy()
cpy.append('lemon')
print(orig)
print(cpy)
['banana', 'cherry', 'apple']
['banana', 'cherry', 'apple', 'lemon']
還可使用list()函數
orig = ['banana', 'cherry', 'apple']
cpy = list(orig)
cpy.append('lemon')
print(orig)
print(cpy)
['banana', 'cherry', 'apple']
['banana', 'cherry', 'apple', 'lemon']
第三種方式是使用切片
orig = ['banana', 'cherry', 'apple']
cpy = orig[:]
cpy.append('lemon')
print(orig)
print(cpy)
['banana', 'cherry', 'apple']
['banana', 'cherry', 'apple', 'lemon']
使用列表生成式可以從一個列表生成另一個列表
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myList2 = [n * n for n in myList]
print(myList)
print(myList2)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 9, 16, 25, 36, 49, 64, 81]