溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中怎樣操作列表

發(fā)布時間:2021-08-26 10:55:15 來源:億速云 閱讀:98 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Python中怎樣操作列表的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1.遍歷列表

需要對列表中的每個元素都執(zhí)行相同的操作時,可使用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 中的第一個值('alice' ),并將其存儲到變量magician 中。接下來,Python讀取下一行代碼:

print(magician)

它讓Python打印magician 的值——依然是'alice' 。鑒于該列表還包含其他值,Python返回到循環(huán)的第一行:

for magician in magicians:

Python獲取列表中的下一個名字——'david' ,并將其存儲到變量magician 中,再執(zhí)行下面這行代碼: 

print(magician)

以此類推,直至列表的最后一個元素。

對列表中的每個元素,都將執(zhí)行循環(huán)指定的步驟,而不管列表包含多少個元素。如果列表包含一百萬個元素,Python就重復(fù)執(zhí)行指定的步驟一百萬次,且通常速度非??臁?nbsp;使用for 循環(huán)處理數(shù)據(jù)是一種對數(shù)據(jù)集執(zhí)行整體操作的不錯的方式。

2.避免縮進(jìn)錯誤,Python根據(jù)縮進(jìn)來判斷代碼行與前一個代碼行的關(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)后的冒號

 for 語句末尾的冒號告訴Python,下一行是循環(huán)的第一行。如果你不小心遺漏了冒號,將導(dǎo)致語法錯誤。

3.創(chuàng)建數(shù)值列表

3.1函數(shù)range()

for value in range(1,5):
  print(value)
>>>1
>>>2
>>>3
>>>4

函數(shù)range()讓Python從你指定的第一個值開始數(shù),在到達(dá)你指定的第二個值后停止,因此輸出并不包含第二值。

3.2使用range()創(chuàng)建數(shù)字列表

將range() 作為list() 的參數(shù),輸出將為一個數(shù)字列表。

numbers = list(range(1,6))
print(numbers)
>>>[1, 2, 3, 4, 5]

range()函數(shù)還可指定步長:

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)建新元素的代碼合并成一行,并自動附加新元素:

squares = [value**2 for value in range(1,11)]
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

首先,指定一個描述性的列表名,如squares。然后指定一個左方括號,并定義一個表達(dá)式,用于生成你要存儲到列表中的值。在這個示例中,表達(dá)式為value**2 ,它計算平方值。接下來,編寫一個for 循環(huán),用于給表達(dá)式提供值,再加上右方括號。在這個示例中,for 循環(huán)為for value in range(1,11) ,它將值1~10提供給表達(dá)式value**2 。請注意,這里的for 語句末尾沒有冒號。

5.列表切片(處理部分列表元素)

與range()一樣,指定要使用的第一個元素和最后一個元素的索引,到達(dá)指定的第二個索引值前面的元素后停止。

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ù)時,可使用切片來進(jìn)行批量處理;編寫Web應(yīng)用程序時,可使用切片來分頁顯示信息。

7.復(fù)制列表

要復(fù)制列表,可創(chuàng)建一個包含整個列表的切片,方法是同時省略起始索引和終止索引([:] )。

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指向同一個列表
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.元組

列表是可以修改的,然而,需要創(chuàng)建一系列不可修改的元素,元組可以滿足這種需求。不可變的列表被稱為元組 。

元組看起來猶如列表,但使用圓括號而不是方括號來標(biāo)識。

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修改元組變量

元組元素不可更改,但可給存儲元組的變量賦值。

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

相比于列表,元組是更簡單的數(shù)據(jù)結(jié)構(gòu)。如果需要存儲的一組值在程序的整個生命周期內(nèi)都不變,可使用元組。

感謝各位的閱讀!關(guān)于“Python中怎樣操作列表”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI