培訓(xùn)啦 Python

Pyhton數(shù)據(jù)結(jié)構(gòu)列表在線學(xué)習(xí)

教培參考

教育培訓(xùn)行業(yè)知識(shí)型媒體

發(fā)布時(shí)間: 2025年05月22日 22:31

2025年【Python】報(bào)考條件/培訓(xùn)費(fèi)用/專(zhuān)業(yè)咨詢(xún) >>

Python報(bào)考條件是什么?Python培訓(xùn)費(fèi)用是多少?Python專(zhuān)業(yè)課程都有哪些?

點(diǎn)擊咨詢(xún)

在Python中,列表(Lists)是很常見(jiàn)的數(shù)據(jù)結(jié)構(gòu)。今天我們要來(lái)在線學(xué)習(xí)列表的相關(guān)知識(shí)點(diǎn),主要內(nèi)容有列表常用操作方法、堆棧、隊(duì)列、列表推導(dǎo)、列表內(nèi)置推導(dǎo)以及del表達(dá)式。一起來(lái)看看吧~

Pyhton數(shù)據(jù)結(jié)構(gòu)列表

1、列表常用操作方法

list.append(x)

list末尾追加元素 x,等價(jià)于 a[len(a):]=[x]

list.extend(iterable)

list末尾追加可迭代類(lèi)型元素(如添加[1,2])等價(jià)于 a[len(a):]=iterable

list.insert(i,x)

list指定位置i添加元素x

list.remove(x)

list中刪除元素

list.pop([i])

從指定位置[i]處刪除元素,未指定位置時(shí),默認(rèn)從末尾元素刪除。

list.clear()

清空l(shuí)ist數(shù)據(jù)

list.index(x[,start[,end]])

返回x在list中首次出現(xiàn)的位置,start,end 指定查找的范圍。

list.count(x)

返回x在list中的個(gè)數(shù)

list.sort(key=None,reverse=False)

對(duì)list進(jìn)行排序

list.reverse()

list元素倒序

list.copy()

返回list的復(fù)制數(shù)據(jù),等價(jià)于a[:]

>>> fruits = ['orange','apple','pear','banana','kiwi','apple','banana']

>>> fruits.count('apple')

2

>>> fruits.count('tangerine')

0

>>> fruits.index('banana')

3

>>> fruits.index('banana',4) # Find next banana starting a position 4

6

>>> fruits.reverse()

>>> fruits

['banana','apple','kiwi','banana','pear','apple','orange']

>>> fruits.append('grape')

>>> fruits

['banana','apple','kiwi','banana','pear','apple','orange','grape']

>>> fruits.sort()

>>> fruits

['apple','apple','banana','banana','grape','kiwi','orange','pear']

>>> fruits.pop()

'pear'

2、列表作為堆棧

堆棧的原則是數(shù)據(jù) 先進(jìn)后出

>>> stack = [3,4,5]

>>> stack.append(6)

>>> stack.append(7)

>>> stack

[3,4,5,6,7]

>>> stack.pop()

7

>>> stack

[3,4,5,6]

>>> stack.pop()

6

>>> stack.pop()

5

>>> stack

[3,4]

3、列表作為隊(duì)列

隊(duì)列的原則是數(shù)據(jù) 先進(jìn)先出

>>> from collections import deque

>>> queue = deque(["Eric","John","Michael"])

>>> queue.append("Terry") # Terry arrives

>>> queue.append("Graham") # Graham arrives

>>> queue.popleft() # The first to arrive now leaves

'Eric'

>>> queue.popleft() # The second to arrive now leaves

'John'

>>> queue # Remaining queue in order of arrival

deque(['Michael','Terry','Graham'])

4、列表推導(dǎo)

根據(jù)List提供的相關(guān)方法,我們可以自己根據(jù)需求創(chuàng)建list

>>> squares = []

>>> for x in range(10):

... squares.append(x**2)

...

>>> squares

[0,1,4,9,16,25,36,49,64,81]

也可以使用lambda表達(dá)式來(lái)創(chuàng)建

squares = list(map(lambda x: x**2,range(10)))

或者更直接

squares = [x**2 for x in range(10)]

使用多個(gè)for循環(huán)或者if 組合語(yǔ)句也可以創(chuàng)建list

>>> [(x,y) for x in [1,2,3] for y in [3,1,4] if x != y][(1,3),(1,4),(2,3),(2,1),(2,4),(3,1),(3,4)]

等價(jià)于

>>> combs = []

>>> for x in [1,2,3]:

... for y in [3,1,4]:

... if x != y:

... combs.append((x,y))

...

>>> combs

[(1,3),(1,4),(2,3),(2,1),(2,4),(3,1),(3,4)]

如果表達(dá)式為元組類(lèi)型,使用時(shí)必須用 ()

>>> vec = [-4,-2,0,2,4]

>>> # create a new list with the values doubled

>>> [x*2 for x in vec]

[-8,-4,0,4,8]

>>> # filter the list to exclude negative numbers

>>> [x for x in vec if x >= 0]

[0,2,4]

>>> # apply a function to all the elements

>>> [abs(x) for x in vec]

[4,2,0,2,4]

>>> # call a method on each element

>>> freshfruit = [' banana',' loganberry ','passion fruit ']

>>> [weapon.strip() for weapon in freshfruit]

['banana','loganberry','passion fruit']

>>> # create a list of 2-tuples like (number,square)

>>> [(x,x**2) for x in range(6)]

[(0,0),(1,1),(2,4),(3,9),(4,16),(5,25)]

>>> # the tuple must be parenthesized,otherwise an error is raised

>>> [x,x**2 for x in range(6)]

File "<stdin>",line 1,in <module>

[x,x**2 for x in range(6)]

^

SyntaxError: invalid syntax

>>> # flatten a list using a listcomp with two 'for'

>>> vec = [[1,2,3],[4,5,6],[7,8,9]]

>>> [num for elem in vec for num in elem]

[1,2,3,4,5,6,7,8,9]

lists推導(dǎo)也可以用混合表達(dá)式和內(nèi)置函數(shù)

>>> from math import pi

>>> [str(round(pi,i)) for i in range(1,6)]

['3.1','3.14','3.142','3.1416','3.14159']

5、列表內(nèi)置推導(dǎo)

下面是一個(gè) 3* 4的矩陣

>>> matrix = [

... [1,2,3,4],

... [5,6,7,8],

... [9,10,11,12],

... ]

下面方法可以將matrix數(shù)據(jù)轉(zhuǎn)換為 4*3的矩陣。

>>> [[row[i] for row in matrix] for i in range(4)]

[[1,5,9],[2,6,10],[3,7,11],[4,8,12]]

等價(jià)于

>>> transposed = []

>>> for i in range(4):

... transposed.append([row[i] for row in matrix])

...

>>> transposed

[[1,5,9],[2,6,10],[3,7,11],[4,8,12]]

或者是這樣

>>> transposed = []

>>> for i in range(4):

... # the following 3 lines implement the nested listcomp

... transposed_row = []

... for row in matrix:

... transposed_row.append(row[i])

... transposed.append(transposed_row)

...

>>> transposed

[[1,5,9],[2,6,10],[3,7,11],[4,8,12]]

上面操作完全可以使用list的內(nèi)置函數(shù) zip() 來(lái)實(shí)現(xiàn)

>>> list(zip(*matrix))

[(1,5,9),(2,6,10),(3,7,11),(4,8,12)]

6、del表達(dá)式

使用可以直接刪除List中的某個(gè)數(shù)值

>>> a = [-1,1,66.25,333,333,1234.5]

>>> del a[0]

>>> a

[1,66.25,333,333,1234.5]

>>> del a[2:4]

>>> a

[1,66.25,1234.5]

>>> del a[:]

>>> a

[]

也可以刪除整個(gè) list

>>> del a

Pyhton數(shù)據(jù)結(jié)構(gòu)列表的學(xué)習(xí)內(nèi)容就分享到這里了,相信大家對(duì)于Pyhton還有很多的疑惑,現(xiàn)在就在教育培訓(xùn)網(wǎng)報(bào)名在線學(xué)習(xí)Python,就有名師一對(duì)一對(duì)你進(jìn)行輔導(dǎo)。還等什么,現(xiàn)在就來(lái)體驗(yàn)吧~

溫馨提示:
本文【Pyhton數(shù)據(jù)結(jié)構(gòu)列表在線學(xué)習(xí)】由作者教培參考提供。該文觀點(diǎn)僅代表作者本人,培訓(xùn)啦系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)空間服務(wù),若存在侵權(quán)問(wèn)題,請(qǐng)及時(shí)聯(lián)系管理員或作者進(jìn)行刪除。
我們采用的作品包括內(nèi)容和圖片部分來(lái)源于網(wǎng)絡(luò)用戶投稿,我們不確定投稿用戶享有完全著作權(quán),根據(jù)《信息網(wǎng)絡(luò)傳播權(quán)保護(hù)條例》,如果侵犯了您的權(quán)利,請(qǐng)聯(lián)系我站將及時(shí)刪除。
內(nèi)容侵權(quán)、違法和不良信息舉報(bào)
Copyright @ 2025 培訓(xùn)啦 All Rights Reserved 版權(quán)所有.