python学习详解_深入解析Python小白学习【操作列表】
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 中。接下來,Python讀取下一行代碼:
print(magician)
它讓Python打印magician 的值——依然是'alice' 。鑒于該列表還包含其他值,Python返回到循環(huán)的第一行:
for magician in magicians:
Python獲取列表中的下一個(gè)名字——'david' ,并將其存儲(chǔ)到變量magician 中,再執(zhí)行下面這行代碼:
print(magician)
以此類推,直至列表的最后一個(gè)元素。
對(duì)列表中的每個(gè)元素,都將執(zhí)行循環(huán)指定的步驟,而不管列表包含多少個(gè)元素。如果列表包含一百萬個(gè)元素,Python就重復(fù)執(zhí)行指定的步驟一百萬次,且通常速度非???。?使用for 循環(huán)處理數(shù)據(jù)是一種對(duì)數(shù)據(jù)集執(zhí)行整體操作的不錯(cuò)的方式。
2.避免縮進(jìn)錯(cuò)誤,Python根據(jù)縮進(jìn)來判斷代碼行與前一個(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 語句末尾的冒號(hào)告訴Python,下一行是循環(huán)的第一行。如果你不小心遺漏了冒號(hào),將導(dǎo)致語法錯(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è)值開始數(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開始數(shù),然后不斷地加2,直到達(dá)到或超過終值。
使用函數(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ì)算平方值。接下來,編寫一個(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 語句末尾沒有冒號(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í),可使用切片來進(jìn)行批量處理;編寫Web應(yīng)用程序時(shí),可使用切片來分頁顯示信息。
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)建一系列不可修改的元素,元組可以滿足這種需求。不可變的列表被稱為元組 。
元組看起來猶如列表,但使用圓括號(hào)而不是方括號(hào)來標(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
總結(jié)
以上是生活随笔為你收集整理的python学习详解_深入解析Python小白学习【操作列表】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythonide机制_强化vim打造p
- 下一篇: python 使用文本注解绘制树节点_整