Python3 基础学习笔记 C03【操作列表】
CSDN 課程推薦:《8小時Python零基礎輕松入門》,講師齊偉,蘇州研途教育科技有限公司CTO,蘇州大學應用統計專業碩士生指導委員會委員;已出版《跟老齊學Python:輕松入門》《跟老齊學Python:Django實戰》、《跟老齊學Python:數據分析》和《Python大學實用教程》暢銷圖書。
Python3 基礎學習筆記第三章【操作列表】
目錄
- 【3.1】遍歷整個列表
- 【3.1.1】在for循環中執行更多的操作
- 【3.2】range()函數
- 【3.2.1】對數字列表執行簡單的統計計算
- 【3.2.2】列表解析
- 【3.3】使用列表的一部分
- 【3.3.1】切片
- 【3.3.2】遍歷列表
- 【3.3.3】復制列表
- 【3.4】元組
- 【3.4.1】定義元組
- 【3.4.2】遍歷元組中所有的值
- 【3.4.3 】修改元組變量
【3.1】遍歷整個列表
使用 for 循環來遍歷整個列表:
names = ['alice' , 'david' , 'liwei']for name in names:print(name)輸出結果如下:
alice david liweifor循環讓Python從列表names中取出一個名字,并將其儲存在變量name中,最后 讓Python打印前面儲存到變量name中的名字,對于列表中的每個名字,Python都將 重復執行后兩行代碼,將列表names中的每個名字都打印出來
【3.1.1】在for循環中執行更多的操作
在for循環中,可對每個元素執行任何操作,下面對前面的示例進行擴展:
例一:
names = ['alice' , 'david' , 'liwei'] for name in names:print(name.title() + ", that was a good man!")輸出結果如下:
Alice, that was a good man! David, that was a good man! Liwei, that was a good man!例二:
names = ['alice' , 'david' , 'liwei'] for name in names:print(name.title() + ", that was a good man!")print("I can't wait to see you again," + name.title() + ".\n") print("Nice to meet you!")輸出結果如下:
Alice, that was a good man! I can't wait to see you again,Alice.David, that was a good man! I can't wait to see you again,David.Liwei, that was a good man! I can't wait to see you again,Liwei.Nice to meet you!【3.2】range()函數
Python使用range()函數能夠輕松地生成一系列的數字
Python3 range() 函數返回的是一個可迭代對象(類型是對象),而不是列表類型, 所以打印的時候不會打印列表;
Python3 list() 函數是對象迭代器,可以把range()返回的可迭代對象轉為一個列表,返回的變量類型為列表;
Python2 range() 函數返回的是列表
例一:
for i in range(1,5):print(i)輸出結果如下:
1 2 3 4例二:
for i in range(5):print(i)輸出結果如下:
0 1 2 3 4例三:
>>> list(range(5)) [0, 1, 2, 3, 4] >>> list(range(0)) [] >>>list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] >>> list(range(0, 10, 2)) [0, 2, 4, 6, 8] >>> list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> list(range(1, 0)) []例四:
squares = [] for value in range(1,11):square = value ** 2squares.append(square) print(squares)輸出結果如下:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]【3.2.1】對數字列表執行簡單的統計計算
>>> digits = [1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0]>>> min(digits)0>>>max(digits)9>>>sum(digits)45【3.2.2】列表解析
列表解析能夠讓比如3.2中的例四更加簡化,只需要一行代碼就能生成這樣的列表,列表解析將for循環和創建新元素的代碼合并成一行,并自動附加新元素:
squares = [value ** 2 for value in range(1,11)]print(squares)在這個示例中,for循環為for value in range(1,11),它將值1~10提供給表達式value ** 2
輸出結果如下:
【3.3】使用列表的一部分
處理列表的部分元素——Python稱之為切片
【3.3.1】切片
list = ['a','b','c','d','e','f'] print(list[:]) #省略全部,代表截取全部內容,可以用來將一個列表拷給另一個列表 print(list[:3]) #省略起始位置的索引,默認起始位置從頭開始,結束位置索引為2 print(list[3:]) #省略結束位置的索引,默認結束位置為最后一個,開始位置索引為3 print(list[1:4]) #開始位置索引為1,結束位置索引為3,顧頭不顧尾 print(list[4:1]) #從左到右索引,因此為空值 print(list[-1:-3]) #從左到右索引,因此為空值 print(list[-3:-1]) #開始位置索引為倒數第三個,結束位置索引為倒數第二個 print(list[1:5:2]) #開始位置索引為1,結束位置索引為4,間隔2 print(list[5:1:-1]) #反向取值,開始位置索引為5,結束位置索引為2 print(list[::-1]) #反向取值,反向輸出列表【3.3.2】遍歷列表
players = ['charles' , 'martina' , 'michael' , 'florence' , 'eli'] print("Here are the first three players on my team:") for player in players[:3]:print(player.title())輸出結果如下:
Here are the first three players on my team: Charles Martina Michael【3.3.3】復制列表
要復制列表,可以創建一個包含整個列表的切片,方法是同時省略起始索引和終止索引([:]),這讓Python創建一個始于第一個元素,終止于最后一個元素的切片,即復制整個列表:
my_foods = ['pizza' , 'falafel' , 'carrot cake'] friend_foods = my_foods[:] print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)輸出結果如下:
My favorite foods are: ['pizza', 'falafel', 'carrot cake']My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake']為核實我們的確有兩個列表,下面在每個列表中都添加一種食品,并核實每個列表都記錄了相應人員喜歡的食品:
my_foods = ['pizza' , 'falafel' , 'carrot cake'] friend_foods = my_foods[:]my_foods.append('cannoli') friend_foods.append('ice cream')print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)輸出結果如下:
My favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli']My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake', 'ice cream']輸出結果表明,'cannoli’包含在我喜歡的食品列表中,而’ice cream’沒有;'ice cream’包含在我朋友喜歡的食品中,而’cannoli’沒有,假如我們只是簡單的將my_foods賦給friend_foods,就不能得到兩個列表。下面是錯誤示例:
my_foods = ['pizza' , 'falafel' , 'carrot cake'] friend_foods = my_foods #錯誤寫法my_foods.append('cannoli') friend_foods.append('ice cream')print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)錯誤示例輸出結果如下:
My favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']【3.4】元組
Python將不能修改的值稱為不可變的,而不可變的列表被稱為元組
【3.4.1】定義元組
元組看起來就像是列表,但元組使用圓括號而不是方括號來標識,定義元組后,就可以使用索引來訪問其元素,就像訪問列表元素一樣:
dimensions = (200,50) print(dimensions[0]) print(dimensions[1])輸出結果如下:
200 50如果嘗試修改元組中元素的值,將會導致Python返回類型錯誤消息,由于試圖修改元組的操作是被禁止的,因此Python指出不能給元組的元素賦值:
dimensions = (200,50) dimensions[0] = 300將會報錯:
Traceback (most recent call last):File "dimensions.py", line 2, in <module>dimensions[0] = 300 TypeError: 'tuple' object does not support item assignment【3.4.2】遍歷元組中所有的值
像列表一樣,元組也可以使用for循環來遍歷元組中的所有值:
例一:
dimensions = (200,100,50,6) for dimension in dimensions:print(dimension)輸出結果如下:
200 100 50 6例二:
dimensions = (200,100,50,6) for dimension in dimensions[:3]:print(dimension)輸出結果如下:
200 100 50【3.4.3 】修改元組變量
雖然不能修改元組元素,但是可以給儲存元組的變量賦值:
dimensions = (200,50) print("Original dimensions:") for dimension in dimensions:print(dimension)dimensions = (400,100) print("\nModified dimensions:") for dimension in dimensions:print(dimension)輸出結果如下:
Original dimensions: 200 50Modified dimensions: 400 100我們首先定義了一個元組,并將其儲存的尺寸打印了出來;然后將一個新元組儲存到變量dimensions中,打印新的尺寸;相比于列表,元組是更簡單的數據結構。如果需要儲存的一組值在程序的整個生命周期內都不變,可使用元組
總結
以上是生活随笔為你收集整理的Python3 基础学习笔记 C03【操作列表】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PWN-PRACTICE-BUUCTF-
- 下一篇: 【Python 必会技巧】使用 spli