CAD小姐姐學Python,學習筆記第三篇,列表的使用
今天的學習筆記是關于列表的,列表是Python中最基本數(shù)據(jù)結構,也是比較常用的Python數(shù)據(jù)類型,列表是一個數(shù)據(jù)的集合,集合內(nèi)可以放任何數(shù)據(jù)類型,可以對序列執(zhí)行增、刪、改操作,對象地址不發(fā)生更改。下面就讓我們學習下關于列表及相關應用吧。
列表
變量可以存儲一個元素,而列表是一個大“容器”,可以存儲N多個元素,程序可以方便地對這些數(shù)據(jù)進行整體操作
列表相當于其它語言中的數(shù)組
列表的創(chuàng)建
1.列表需要使用中括號[],元素之間使用英文的逗號進行分隔
2.調(diào)用內(nèi)置函數(shù)list()
列表的特點
1.列表元素按順序有序排列
2.索引映射唯一一個數(shù)據(jù)
3.列表可以存儲重復數(shù)據(jù)
4.任意數(shù)據(jù)類型混合
3.根據(jù)需要動態(tài)分配和回收內(nèi)存
列表的查詢操作
1.獲取列表中指定元素的索引 ? index():
1.如果列表中存在N個相同元素,只返回相同元素中的第一個元素的索引
2.如果查詢的元素在列表中不存在,則會拋出ValueError
3.還可以在制定的start和stop之間進行查找
例1:
lst=['hello','python','98','hello']
print(lst.index('hello'))
0
例2:
lst=(['hello','python','world'])
print(lst.index('Baby'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13648/1175636271.py in
1 lst=(['hello','python','world'])
----> 2 print(lst.index('Baby'))
ValueError: 'Baby' is not in list
例3:
lst=['hello','python','98','hello']
print(lst.index('hello',1,4))
3
2.獲取列表中的單個元素
1.正向索引從0到N-1,舉例:lst[0]
2.逆向索引從-N到-1,舉例:lst[-N]
3.指定索引不存,拋出indexError
lst=['hello','world',98,'hello','world',234]
print(lst[2])????????????#獲取索引為2的元素
98
lst=['hello','world',98,'hello','world',234]
print(lst[-3])????????????#獲取索引為-3的元素
hello
lst=['hello','world',98,'hello','world',234]
print(lst[6])????????????#獲取索引為6的元素,指定索引不在,報錯
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13648/2375259125.py in
1 lst=['hello','world',98,'hello','world',234]
----> 2 print(lst[6]) #獲取索引為6的元素
IndexError: list index out of range
3.獲取列表中的多個元素
語法格式:
列表名[start:stop:step]
切片操作:1.切片的結果(原列表片段的拷貝)
2.切片的范圍 [start,stop)
3.step默認為1(簡寫為[start:stop]
4.step為正數(shù):[:stop:step] 切片的第一個元素默認是列表的第一個元素 (從start開始往后計算切片)
[start::step] 切片的最后一個元素默認是列表的最后一個元素
5.step為負數(shù) [:stop:step] 切片的第一個元素默認是列表的最后一個元素(從start開始往前計算切片)
[start::step] 切片的最后一個元素默認是列表的第一個元素
例:切片的范圍、切片的結果
lst=[10,20,30,40,50,60,70,80]
print('原列表',(lst))
lst2=lst[1:6:1]????????????????#start=1,stop=6,step1
print('切的片段:',(lst2))
原列表 [10, 20, 30, 40, 50, 60, 70, 80]
[20, 30, 40, 50, 60]
切的片段: [20, 30, 40, 50, 60]
例:step默認為1
lst=[10,20,30,40,50,60,70,80]
print(lst[1:6:1])????#start=1,stop=6,step1
print(lst[1:6])??????#start=1,stop=6,默認步長為1
print(lst[1:6:])?????#start=1,stop=6,默認步長為1
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
例:step為正數(shù)
lst=[10,20,30,40,50,60,70,80]
print(lst[1:6:2])?????#start=1,stop=6,step2
print(lst[:6:2])??????#start從最初開始,stop=6,step2
print(lst[1::2])??????#start=1,stop到最后,默認步長為1
[20, 40, 60]
[10, 30, 50]
[20, 40, 60, 80]
例:step為負數(shù)
lst=[10,20,30,40,50,60,70,80]
print('原列表',lst)
print(lst[::-1])??????#start=7(最后一個),stop=0,step?=?-1
print(lst[7::-1])??????#start=7,stop=0,step?=?-1
print(lst[6:0:-2])??????#start=6,stop到最后不包括0,step?=?-2
原列表 [10, 20, 30, 40, 50, 60, 70, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]
4.判斷指定元素在列表中是否存在
1.判斷指定定元素在列表中是否存在
元素 in 列表名
元素 not in 列表名
2.列表元素的遍歷
for 迭代變量 in 列表名:
操作
例1:
lst=[10,20,'python','hello']
print(10?in?lst)
print(100?in?lst)
print(10?not?in?lst)
print(100?not?in?lst)
True
False
False
True
例2:
lst=[10,20,'python','hello']
for?item?in?lst:
????print(item)
10
20
python
hello
5.列表元素的增加
操作方法:
append() :在列表的末尾添加一個元素
extend() :在列表的末尾至少添加一個元素
insert() :在列表的任意位置添加一個元素
切片 :在列表的任意位置添加至少一個元素
例:向列表的末尾添加一個元素
lst=[10,20,30]
print('添加元素前',lst)
lst.append(40)
print('添加元素后',lst)
print('添加元素前',lst,id(lst))
print('添加元素后',lst,id(lst))??id?相同說明還是同一個列表,只是在末尾增加了一個元素
添加元素前 [10, 20, 30]
添加元素后 [10, 20, 30, 40]
添加元素前 [10, 20, 30, 40] 2418471799232
添加元素后 [10, 20, 30, 40] 2418471799232
例:向列表的末尾至少添加一個元素
lst=[10,20,30]
lst2=['hello','python']
lst.append(lst2)?????????#將lst2作為一個元素添加到lst末尾
print(lst)????????????????????????
[10, 20, 30, ['hello', 'python']]
lst=[10,20,30]
lst2=['hello','python']
lst.extend(lst2)?????????#將lst2的每個元素添加到lst末尾,向列表的末尾添加多個元素
print(lst)??????
[10, 20, 30, 'hello', 'python']
例3:在列表指定的位置添加元素
lst=[10,20,30]
lst.insert(1,40)?????#在索引為1的位置添加40
print(lst)
[10, 40, 20, 30]
例4:切片然后在任意位置添加N多個元素
lst=[10,20,30]
lst3=[True,False,'hello']
lst[1:]=lst3????????????#從索引為1的位置開始到最后切片并替換添加新元素
print(lst)
[10, True, False, 'hello']
6.列表元素的刪除
操作方法:
1.remove() :一次刪除一個元素
重復元素只刪除第一個
元素不存在拋出ValueError
2.pop() :刪除一個指定索引位置上的元素
指定索引不存在拋出IndexError
不指定索引,刪除列表中最后一個元素
3.切片 :一次至少刪除一個元素
4.clear :清空列表
5.del :刪除列表
例1:remove()
lst=[10,20,30,40,50,30]
lst.remove(30)
print(lst)???????????????????#從列表中移除一個元素,如果有重復元素只移除第一個元素
lst.remove(100)??????????????#報錯元素不存在
[10, 20, 40, 50, 30]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8992/2932006073.py in
2 lst.remove(30)
3 print(lst) #從列表中移除一個元素,如果有重復元素只移除第一個元素
----> 4 lst.remove(100)
ValueError: list.remove(x): x not in list
例2:pop()
lst=[10,20,30,40,50,30]
lst.pop(1)?????????????????#從列表中移除索引位置是1的元素?
print(lst)
lst.pop(7)??????????????????#從列表中移除索引位置是7的元素,不存在7的元素報錯異常
print(lst)
[10, 30, 40, 50, 30]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12892/2040156885.py in
2 lst.pop(1) #從列表中移除索引位置是1的元素
3 print(lst)
----> 4 lst.pop(7) #從列表中移除索引位置是7的元素,不存在7的元素報錯異常
5 print(lst)
IndexError: pop index out of range
lst=[10,20,30,40,50,30]
lst.pop()??????????????????#不指定索引,刪除列表中最后一個元素
print(lst)
[10, 20, 30, 40, 50]
例3:切片
lst=[10,20,30,40,50,30]
new_list=lst[1:3]??????????????#切片操作刪除至少一個元素,將產(chǎn)生一個新的列表對象
print('原列表',lst)
print('切片后的列表',new_list)
原列表 [10, 20, 30, 40, 50, 30]
切片后的列表 [20, 30]
lst=[10,20,30,40,50,30]
lst[1:3]=[]??????????????#切片操作刪除原列表中的內(nèi)容,不產(chǎn)生新的列表,把索引的1和2的元素刪除
print(lst)
[10, 40, 50, 30]
例4:clear
lst=[10,20,30,40,50,30]
lst.clear()???????#清除列表中的所有元素
print(lst)??????????
[]
例5:del
lst=[10,20,30,40,50,30]
del?lst??????#直接清除列表對象
print(lst)????#報錯???'lst'?is?not?defined????
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8992/1850059668.py in
1 lst=[10,20,30,40,50,30]
2 del lst
----> 3 print(lst)
NameError: name 'lst' is not defined
7.列表元素的修改操作
1.為指定索引的元素賦予一個新值
2.為指定的切片賦予一個新值
例1:
lst=[10,20,30,40]
lst[2]=100????????#修改索引為2的元素換成100
print(lst)
[10, 20, 100, 40]
例2:
lst=[10,20,30,40]
lst[1:2]=[50,60,70,80]???#索引1和2的值換成50,60,70,80
print(lst)
[10, 50, 60, 70, 80, 30, 40]
8.列表元素的排序操作
1.調(diào)用sort()方法,列表中的所有元素默認按照從小到大的順序進行排序,可以指定reverse=True,進行降序排序
2.調(diào)用內(nèi)置函數(shù)sorted(),可以指定reverse=True,進行降序排序,原列表不發(fā)生改變,排序后產(chǎn)生一個新的列表對象
例1:sort(),對原列表進行排序
lst=[20,40,10,98,54]
print('排序前的列表',lst,id(lst))??
lst.sort()????????????????????????????#通過指定關鍵字參數(shù),將列表中的元素進行升序排序
print('排序后的列表',lst,id(lst))?????#id?相同說明原列表對象不變,只是做個排序處理
lst.sort(reverse=True)???????????????#reverse=True?表示降序排列
print(lst)
lst.sort(reverse=False)???????????????#reverse=False?表示降序排列
print(lst)
排序前的列表 [20, 40, 10, 98, 54] 2500174974080
排序后的列表 [10, 20, 40, 54, 98] 2500174974080
[98, 54, 40, 20, 10]
[10, 20, 40, 54, 98]
例2:sorted(),原列表不變,排序后產(chǎn)生一個新的列表對象
lst=[20,40,10,98,54]
print(lst)
new_list=sorted(lst)????#升序排序,排序后的是一個新的列表對象
print(new_list)
new_list=sorted(lst,reverse=True)???#降序排序,排序后是一個新的列表對象
print(new_list)
[20, 40, 10, 98, 54]
[10, 20, 40, 54, 98]
[98, 54, 40, 20, 10]
9.列表的復制
list.copy() :復制列表
例:
lst=[10,20,30,'Baby',50,30,60,30]
lst3=lst2=lst.copy()???????#lst2和lst3復制lst列表
print(lst2)??????????????
print(lst3)
[10, 20, 30, 'Baby', 50, 30, 60, 30]
[10, 20, 30, 'Baby', 50, 30, 60, 30]
10.列表生成式(生成列表的公式)
語法格式:
[i*i for i in range(1,10)]
i*i 表示列表元素
i 表示自定義變量
range() 可迭代對象
**注意事項:‘表示列表元素的表達式’中通常包含自定義變量
例:
lst=[i?for?i?in?range(1,10)]
print(lst)
lst2=[i*i?for?i?in?range(1,10)]
print(lst2)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
例:列表中的元素是2,4,6,8,10
lst=[i*2?for?i?in?range(1,6)]
print(lst)
[2, 4, 6, 8, 10]
最后,推薦螞蟻老師的Python入門課,限時249元:
