Python使用list一、list
Python內(nèi)置的一種數(shù)據(jù)類型是列表:list。list是一種有序的集合,可以隨時(shí)添加和刪除其中的元素。
比如,列出班里所有同學(xué)的名字,就可以用一個(gè)list表示:
classmates = ['Michael', 'Bob', 'Tracy']print(classmates)
變量classmates就是一個(gè)list。
len()函數(shù)1. 獲得list元素的個(gè)數(shù):classmates = ['Michael', 'Bob', 'Tracy']print(len(classmates))
用索引來訪問list中每一個(gè)位置的元素,記得索引是從0開始的:
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[0])
print(classmates[1])
print(classmates[2])
print(classmates[3])
當(dāng)索引超出了范圍時(shí),Python會(huì)報(bào)一個(gè)IndexError錯(cuò)誤,所以,要確保索引不要越界,記得最后一個(gè)元素的索引是len(classmates) - 1。
如果要取最后一個(gè)元素,除了計(jì)算索引位置外,還可以用-1做索引,直接獲取最后一個(gè)元素:
print(classmates[-1])
以此類推,可以獲取倒數(shù)第2個(gè)、倒數(shù)第3個(gè):
classmates = ['Michael', 'Bob', 'Tracy']
print(classmates[-1])
print(classmates[-2])
print(classmates[-3])
print(classmates[-4])
當(dāng)然,倒數(shù)第4個(gè)就越界了。
2. list是一個(gè)可變的有序表,往list中追加元素到末尾:classmates = ['Michael', 'Bob', 'Tracy']
classmates.a(chǎn)ppend('Adam')
print(classmates)
也可以把元素插入到指定的位置,比如索引號(hào)為1的位置:
classmates = ['Michael', 'Bob', 'Tracy']#替換classmates.insert(1, 'Jack')
print(classmates)
pop()函數(shù)1. 刪除list末尾的元素classmates = ['Michael', 'Bob', 'Tracy']
print(classmates.pop())
print( classmates)['Michael', 'Jack', 'Bob', 'Tracy']
2. 刪除指定位置的元素,用pop(i)方法,其中i是索引位置。
classmates.pop(1)
print(classmates)
3. 把某個(gè)元素替換成別的元素,可以直接賦值給對(duì)應(yīng)的索引位置:
classmates = ['Michael', 'Bob', 'Tracy']
classmates[1] = 'Sarah'
print(classmates)
list里面的元素的數(shù)據(jù)類型也可以不同,比如:
L = ['Apple', 123, True]
list元素也可以是另一個(gè)list,比如:
s = ['python', 'java', ['asp', 'php'], 'scheme']print(len(s))
要注意s只有4個(gè)元素,其中s[2]又是一個(gè)list,如果拆開寫就更容易理解了:
p = ['asp', 'php']s = ['python', 'java', p, 'scheme']
要拿到'php'可以寫p[1]或者s[2][1],因此s可以看成是一個(gè)二維數(shù)組,類似的還有三維、四維……數(shù)組,不過很少用到。
如果一個(gè)list中一個(gè)元素也沒有,就是一個(gè)空的list,它的長(zhǎng)度為0:
L = []len(L)二、總結(jié)
本文基于Python基礎(chǔ),主要介紹了Python基礎(chǔ)中l(wèi)ist列表,通過list列表的兩個(gè)函數(shù) ,對(duì)list的語法做了詳細(xì)的講解,用豐富的案例 ,代碼效果圖的展示幫助大家更好理解 。
使用Python編程語言,方便大家更好理解,希望對(duì)大家的學(xué)習(xí)有幫助。
-
可編程邏輯
+關(guān)注
關(guān)注
7文章
517瀏覽量
44191 -
python
+關(guān)注
關(guān)注
56文章
4809瀏覽量
85071
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
對(duì)比Python與Java編程語言
Linux環(huán)境變量配置方法
技術(shù)干貨驛站 ▏深入理解C語言:基本數(shù)據(jù)類型和變量
![技術(shù)干貨驛站 ▏深入理解C語言:基本數(shù)據(jù)<b class='flag-5'>類型</b>和<b class='flag-5'>變量</b>](https://file1.elecfans.com/web2/M00/FC/CD/wKgZomaWI5uASgBaAABuQHdMO4I302.png)
網(wǎng)絡(luò)爬蟲,Python和數(shù)據(jù)分析
Win10 vscode中無法編譯,提示python.exe: command not found怎么解決?
嵌入式中C語言結(jié)構(gòu)體基本實(shí)現(xiàn)
![嵌入式中C語言結(jié)構(gòu)體基本實(shí)現(xiàn)](https://file1.elecfans.com//web2/M00/E4/42/wKgZomY_FOKAdlRXAADOabNodfI522.jpg)
HarmonyOS開發(fā)案例:【使用List組件實(shí)現(xiàn)設(shè)置項(xiàng)】
![HarmonyOS開發(fā)案例:【使用<b class='flag-5'>List</b>組件實(shí)現(xiàn)設(shè)置項(xiàng)】](https://file1.elecfans.com/web2/M00/DC/EC/wKgZomYsojKAaJ63AIRVv6XhI_A939.jpg)
OpenHarmony語言基礎(chǔ)類庫【@ohos.util.List (線性容器List)】
![OpenHarmony語言基礎(chǔ)類庫【@ohos.util.<b class='flag-5'>List</b> (線性容器<b class='flag-5'>List</b>)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
OpenHarmony實(shí)戰(zhàn)開發(fā)-list開發(fā)指導(dǎo)
關(guān)于go中接口類型的表示方法
鴻蒙TypeScript學(xué)習(xí)第14天:【聯(lián)合類型】
![鴻蒙TypeScript學(xué)習(xí)第14天:【聯(lián)合<b class='flag-5'>類型</b>】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
C語言數(shù)據(jù)類型有哪些
![C語言數(shù)據(jù)<b class='flag-5'>類型</b>有哪些](https://file1.elecfans.com/web2/M00/C5/16/wKgZomX6UKqAb6q4AABHS7gAoYk070.png)
C語言變量應(yīng)用詳解
![C語言<b class='flag-5'>變量</b>應(yīng)用詳解](https://file1.elecfans.com/web2/M00/C4/BC/wKgZomX3sv2AGVWeAABMt9agjcE672.png)
評(píng)論