bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Python中怎樣操作列表-創新互聯

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

創新互聯專注于烏魯木齊企業網站建設,響應式網站,電子商務商城網站建設。烏魯木齊網站建設公司,為烏魯木齊等地區提供建站服務。全流程按需網站開發,專業設計,全程項目跟蹤,創新互聯專業和態度為您提供的服務

1.遍歷列表

需要對列表中的每個元素都執行相同的操作時,可使用for 循環:

magicians = ['alice','david','carolina']
for magician in magicians:
  print(magician)
>>>alice
>>>david
>>>carolina

 循環中,Python將首先讀取其中的第一行代碼:

for magician in magicians:

這行代碼讓Python獲取列表magicians 中的第一個值('alice' ),并將其存儲到變量magician 中。接下來,Python讀取下一行代碼:

print(magician)

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

for magician in magicians:

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

print(magician)

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

對列表中的每個元素,都將執行循環指定的步驟,而不管列表包含多少個元素。如果列表包含一百萬個元素,Python就重復執行指定的步驟一百萬次,且通常速度非常快。 使用for 循環處理數據是一種對數據集執行整體操作的不錯的方式。

2.避免縮進錯誤,Python根據縮進來判斷代碼行與前一個代碼行的關系

2.1未縮進:

magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
IndentationError: expected an indented block

2.2循環后的冒號

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

3.創建數值列表

3.1函數range()

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

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

3.2使用range()創建數字列表

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

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

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

even_numbers = list(range(1,13,2))
print(even_numbers)
>>>[1, 3, 5, 7, 9, 11]

函數range() 從1開始數,然后不斷地加2,直到達到或超過終值。

使用函數range() 幾乎能夠創建任何需要的數字集。

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

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

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

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

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

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 循環中使用切片。

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

處理數據時,可使用切片來進行批量處理;編寫Web應用程序時,可使用切片來分頁顯示信息。

7.復制列表

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

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.元組

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

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

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 循環遍歷元組

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

相比于列表,元組是更簡單的數據結構。如果需要存儲的一組值在程序的整個生命周期內都不變,可使用元組。

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

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網頁標題:Python中怎樣操作列表-創新互聯
文章網址:http://vcdvsql.cn/article46/jiihg.html

成都網站建設公司_創新互聯,為您提供搜索引擎優化網站制作網站導航品牌網站建設軟件開發網站內鏈

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設