列表是最常用的Python數據類型,也是Python中最基本的數據結構,只要在實際應用中,使用列表編程非常頻繁。
目錄
一:創建列表
1.1.創建普通的列表
只要把逗號分隔的不同的數據項使用方括號括起來
list = []
list1 = ['Google', 'TengXun', 'Baidu', 'Alibb']
list2 = [1, 2, 3, 4, 5 ]
list3 = ["hello", "hi", "how are you", "that's good"]
1.2.創建數值列表
使用list()函數將range()函數的結果轉換爲列表
numlist = list(range(10))
print(numlist)
結果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(param1, param2, param3) 第三個參數param3可以指定步長
numlist = list(range(0, 11, 2))
print(numlist)
結果:
[0, 2, 4, 6, 8, 10]
1.3.列表解析創建列表
列表解析 將for 循環和創建新元素的代碼合併成一行,並自動附加新元素
squares = [value**2 for value in range(1, 11)]
print(squares)
# 注意for value in range(1, 11)末尾沒有冒號
結果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
二:列表操作
2.1.訪問列表
通過索引訪問
列表第一個元素的索引是0而不是1
列表最後一個元素可以通過索引-1訪問
names = ['小明','小紅','john', 'ben', 'kitty']
print(names[0])
print(names[-1])
print(names[3])
結果:
小明
kitty
ben
2.2.修改列表元素
通過元素的索引修改,list[index] = ‘new_element’
# 修改列表元素的方法和訪問元素的方法一樣,通過索引去修改
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
結果:
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
2.3.對列表添加元素
1.使用方法append()將元素添加到列表末尾,list.append
2.使用方法insert()將元素插入到列表的指定索引位置,list.insert(new_index, element)
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.append('sanlin')
print(motorcycles)
motorcycles.insert(1, 'motor')
print(motorcycles)
結果:
['ducati', 'yamaha', 'suzuki', 'sanlin']
['ducati', 'motor', 'yamaha', 'suzuki', 'sanlin']
2.4.從列表刪除元素
1.使用del語句刪除元素,需要指定索引,del list[del_index]
2.使用方法pop()刪除元素,並且返回該值,不指定索引時刪除列表末尾的元素,list.pop([pop_index])
3.使用方法remove()可以根據值刪除元素,list.remove(element)
# del函數
motorcycles = ['ducati', 'motor', 'yamaha', 'suzuki', 'sanlin']
del motorcycles[1]
print(motorcycles)
#pop方法
pop_motorcycle = motorcycles.pop() # 不指定索引時刪除最後一個元素
print(pop_motorcycle)
print(motorcycles)
pop_motorcycle1 = motorcycles.pop(1) #指定刪除的索引
print(pop_motorcycle1)
print(motorcycles)
del motorcycles #是將整個列表刪除,再次訪問會報錯
print(motorcycles)
#remove方法
pets = ['dog', 'cat', 'duck', 'squirrel']
pets.remove('duck')
print("屬於寵物的有:")
for pet in pets:
print(pet)
結果:
['ducati', 'yamaha', 'suzuki', 'sanlin']
sanlin
['ducati', 'yamaha', 'suzuki']
yamaha
['ducati', 'suzuki']
NameError: name 'motorcycles' is not defined
屬於寵物的有:
dog
cat
squirrel
三:組織列表
3.1.對列表進行永久性排序
- 使用方法sort()對列表進行永久性排序
- list.sort(reverse = True)按與字符順序相反的順序排列列表元素
cars = ['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
print("排序前:" + str(cars))
# 1. 使用方法sort()對列表進行永久性排序
cars.sort()
print("使用sort排序後:" + str(cars))
# 2. list.sort(reverse = True)按與字符順序相反的順序排列列表元素
cars.sort(reverse = True)
print("使用sort帶reverse選項後:" + str(cars))
結果:
排序前:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
使用sort排序後:['audi', 'bwm', 'honda', 'lexus', 'toyata', 'vx']
使用sort帶reverse選項後:['vx', 'toyata', 'lexus', 'honda', 'bwm', 'audi']
3.2.對列表進行臨時排序
使用函數sorted()對列表進行臨時排序
cars = ['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
print("排序前:" + str(cars))
print("使用函數sorted()後:" + str(sorted(cars)))
print("再次查看列表:" + str(cars))
結果:
排序前:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
使用函數sorted()後:['audi', 'bwm', 'honda', 'lexus', 'toyata', 'vx']
再次查看列表:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
3.3.對列表進行倒着排序
list.reverse()倒着打印列表, 永久性地修改列表元素排序順序,
再次調用list.reverse可恢復原來排序順序
cars = ['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
print("排序前:" + str(cars))
print("倒着排序後:" + str(cars))
print("再次調用reverse:" + str(cars))
結果:
排序前:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
倒着排序後:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
再次調用reverse:['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
3.4.對列表進行統計
函數len() 確定列表的長度
min() 統計列表最大值元素
max() 統計列表最小值元素
sum() 統計列表累加和
cars = ['bwm', 'audi', 'lexus', 'honda', 'vx', 'toyata']
print("cars 數量爲:" + str(len(cars)))
結果:
cars 數量爲:6