深入解析Python小白學(xué)習(xí)【操作列表】
1.遍歷列表
需要對(duì)列表中的每個(gè)元素都執(zhí)行相同的操作時(shí),可使用for 循環(huán):
magicians = ['alice','david','carolina'] for magician in magicians: print(magician) >>>alice >>>david >>>carolina
循環(huán)中,Python將首先讀取其中的第一行代碼:
for magician in magicians:
這行代碼讓Python獲取列表magicians 中的第一個(gè)值('alice' ),并將其存儲(chǔ)到變量magician 中。接下來(lái),Python讀取下一行代碼:
print(magician)
它讓Python打印magician 的值——依然是'alice' 。鑒于該列表還包含其他值,Python返回到循環(huán)的第一行:
for magician in magicians:
Python獲取列表中的下一個(gè)名字——'david' ,并將其存儲(chǔ)到變量magician 中,再執(zhí)行下面這行代碼:
print(magician)
以此類(lèi)推,直至列表的最后一個(gè)元素。
對(duì)列表中的每個(gè)元素,都將執(zhí)行循環(huán)指定的步驟,而不管列表包含多少個(gè)元素。如果列表包含一百萬(wàn)個(gè)元素,Python就重復(fù)執(zhí)行指定的步驟一百萬(wàn)次,且通常速度非???。 使用for 循環(huán)處理數(shù)據(jù)是一種對(duì)數(shù)據(jù)集執(zhí)行整體操作的不錯(cuò)的方式。
2.避免縮進(jìn)錯(cuò)誤,Python根據(jù)縮進(jìn)來(lái)判斷代碼行與前一個(gè)代碼行的關(guān)系
2.1未縮進(jìn):
magicians = ['alice','david','carolina'] for magician in magicians: print(magician)
IndentationError: expected an indented block
2.2循環(huán)后的冒號(hào)
for 語(yǔ)句末尾的冒號(hào)告訴Python,下一行是循環(huán)的第一行。如果你不小心遺漏了冒號(hào),將導(dǎo)致語(yǔ)法錯(cuò)誤。
3.創(chuàng)建數(shù)值列表
3.1函數(shù)range()
for value in range(1,5): print(value) >>>1 >>>2 >>>3 >>>4
函數(shù)range()讓Python從你指定的第一個(gè)值開(kāi)始數(shù),在到達(dá)你指定的第二個(gè)值后停止,因此輸出并不包含第二值。
3.2使用range()創(chuàng)建數(shù)字列表
將range() 作為list() 的參數(shù),輸出將為一個(gè)數(shù)字列表。
numbers = list(range(1,6)) print(numbers) >>>[1, 2, 3, 4, 5]
range()函數(shù)還可指定步長(zhǎng):
even_numbers = list(range(1,13,2)) print(even_numbers) >>>[1, 3, 5, 7, 9, 11]
函數(shù)range() 從1開(kāi)始數(shù),然后不斷地加2,直到達(dá)到或超過(guò)終值。
使用函數(shù)range() 幾乎能夠創(chuàng)建任何需要的數(shù)字集。
squares = [] for value in range(1,11): squares.append(value**2) print(squares) >>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4.列表解析
列表解析將for 循環(huán)和創(chuàng)建新元素的代碼合并成一行,并自動(dòng)附加新元素:
squares = [value**2 for value in range(1,11)] print(squares) >>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
首先,指定一個(gè)描述性的列表名,如squares。然后指定一個(gè)左方括號(hào),并定義一個(gè)表達(dá)式,用于生成你要存儲(chǔ)到列表中的值。在這個(gè)示例中,表達(dá)式為value**2 ,它計(jì)算平方值。接下來(lái),編寫(xiě)一個(gè)for 循環(huán),用于給表達(dá)式提供值,再加上右方括號(hào)。在這個(gè)示例中,for 循環(huán)為for value in range(1,11) ,它將值1~10提供給表達(dá)式value**2 。請(qǐng)注意,這里的for 語(yǔ)句末尾沒(méi)有冒號(hào)。
5.列表切片(處理部分列表元素)
與range()一樣,指定要使用的第一個(gè)元素和最后一個(gè)元素的索引,到達(dá)指定的第二個(gè)索引值前面的元素后停止。
players = ['charles','martina','michael','florence','eli'] print(players[0:3]) >>>['charles', 'martina', 'michael']
未指定起始索引及終止索引的情況:
players = ['charles','martina','michael','florence','eli'] print(players[:4]) >>>['charles', 'martina', 'michael', 'florence']
players = ['charles','martina','michael','florence','eli'] print(players[1:]) >>>['martina', 'michael', 'florence', 'eli']
players = ['charles','martina','michael','florence','eli'] print(players[-3:]) >>>['michael', 'florence', 'eli']
6.遍歷切片
要遍歷列表的部分元素,可在for 循環(huán)中使用切片。
players = ['charles','martina','michael','florence','eli']
print("Here are the first three players in my team:")
for player in players[0:3]:
print(player.title())
>>>Here are the first three players in my team:
>>>Charles
>>>Martina
>>>Michael
處理數(shù)據(jù)時(shí),可使用切片來(lái)進(jìn)行批量處理;編寫(xiě)Web應(yīng)用程序時(shí),可使用切片來(lái)分頁(yè)顯示信息。
7.復(fù)制列表
要復(fù)制列表,可創(chuàng)建一個(gè)包含整個(gè)列表的切片,方法是同時(shí)省略起始索引和終止索引([:] )。
my_foods = ['pizza','falafel','carrot cake'] friend_foods = my_foods[:] print(my_foods) print(friend_foods) >>>['pizza', 'falafel', 'carrot cake'] >>>['pizza', 'falafel', 'carrot cake']
my_foods = ['pizza','falafel','carrot cake']
# friend_foods和my_foods指向同一個(gè)列表
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
>>>['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
>>>['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
8.元組
列表是可以修改的,然而,需要?jiǎng)?chuàng)建一系列不可修改的元素,元組可以滿(mǎn)足這種需求。不可變的列表被稱(chēng)為元組 。
元組看起來(lái)猶如列表,但使用圓括號(hào)而不是方括號(hào)來(lái)標(biāo)識(shí)。
dimensions = (200,50) print(dimensions[0]) print(dimensions[1]) >>>200 >>>50
元組元素不可更改:
dimensions = (200,50) dimensions[0] = 230 >>>dimensions[0] = 230 >>>TypeError: 'tuple' object does not support item assignment
8.1 for 循環(huán)遍歷元組
dimensions = (200,50,100) for dimension in dimensions: print(dimension) >>>200 >>>50 >>>100
8.2修改元組變量
元組元素不可更改,但可給存儲(chǔ)元組的變量賦值。
dimensions = (200,50,100) for dimension in dimensions: print(dimension) dimensions = (50,40,30) for dimension in dimensions: print(dimension) >>>200 >>>50 >>>100 >>>50 >>>40 >>>30
相比于列表,元組是更簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu)。如果需要存儲(chǔ)的一組值在程序的整個(gè)生命周期內(nèi)都不變,可使用元組。
以上所述是小編給大家介紹的Python操作列表詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
python 基于DDT實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試
這篇文章主要介紹了python 基于DDT實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02
python數(shù)據(jù)類(lèi)型相關(guān)知識(shí)擴(kuò)展
今天帶大家學(xué)習(xí)Python數(shù)據(jù)類(lèi)型的擴(kuò)展知識(shí),文中有非常詳細(xì)的介紹介代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴有很大的幫助,需要的朋友可以參考下2021-05-05
Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲(chóng)
這篇文章主要介紹了Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲(chóng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
人工智能學(xué)習(xí)Pytorch數(shù)據(jù)集分割及動(dòng)量示例詳解
這篇文章主要為大家介紹了人工智能學(xué)習(xí)Pytorch數(shù)據(jù)集分割及動(dòng)量示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Python實(shí)現(xiàn)二維數(shù)組輸出為圖片
下面小編就為大家分享一篇Python實(shí)現(xiàn)二維數(shù)組輸出為圖片,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
通過(guò)示例學(xué)習(xí)python中os模塊的使用
os模塊是Python中處理文件和文件夾的重要模塊,其中了解模塊的一些基本功能對(duì)于使用Python對(duì)excel進(jìn)行數(shù)據(jù)分析具有很大的幫助,這篇文章主要介紹了python os模塊使用,感興趣的朋友跟隨小編一起看看吧2022-12-12
對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解
今天小編就為大家分享一篇對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

