哈嘍兄弟們,今天咱們來復習一下Python基礎中的串列操作,不知道各位還記得多少呢?
遍歷整個串列加粗樣式
遍歷串列的所有元素,對每一個元素進行相同的操作,是我們常常所需要的,在python中可使用for()回圈,
假如我們需要將一個串列中的手機品牌一一列印,我們可以分別獲取手機品牌的名字,如果資料特別的,對我們的操作要求量很大,而且容易輸入錯誤,但使用for回圈,python可以幫你解決這些問題,
例如:
shouji=['iphone','huawei','xiaomi','vivo','oppo'] for shoujis in shouji: print(shoujis)
運行結果
iphone
huawei
xiaomi
vivo
oppo
深入研究回圈
回圈這一概念非常重要,它是計算機自動重復完成作業的常見方法之一,
python首先讀取第一行命令 for shoujis in shouji: 這讓python獲取到串列shouji的第一個值,iphone,然后下一行 print(shoujis),于是列印出讀取出的第一個值 iphone ,如此回圈,
在for回圈中執行更多命令
在for回圈中可對每一個元素執行任意操作,下列展示對沒一部手機列印一條訊息
# Python資料原始碼領取q群:708525271 shouji=['iphone','huawei','xiaomi','vivo','oppo'] for shoujis in shouji: print(f"{shoujis.title()} it is a good phone")
運行結果
Iphone it is a good phone Huawei it is a good phone Xiaomi it is a good phone Vivo it is a good phone Oppo it is a good phone
在for回圈后面,每一個縮進的代碼都是回圈的一部分,將針對串列的每一個值都執行一次,
在for回圈結束后執行一些操作
在for回圈后面,沒有縮進的代碼只執行一次,不會重復執行,現在操作一下對于所有手機予以五行好評,代碼不縮進,
# Python資料原始碼領取q群:708525271 shouji=['iphone','huawei','xiaomi','vivo','oppo'] for shoujis in shouji: print(f"{shoujis} it is a good phone") print("我給出五星好評")
運行結果
iphone it is a good phone huawei it is a good phone xiaomi it is a good phone vivo it is a good phone oppo it is a good phone 我給出五星好評
開始撰寫正確縮進代碼時,需要注意常見的縮進錯誤,對于for回圈部分且屬于組成回圈的代碼行,一定要縮進!有時候忘記縮進代碼會運行且不報告錯誤,結果很可能出乎你的意料,
使用函式range()
range()可以輕松的幫助你生產一系列數
例如:
for value in range(1,5): print(value)
運行結果
1 2 3 4
上述代碼好像列印1到5,實際并不列印5,這是編輯語言中常見的差一行行為的結果,如果需要列印5,則需要使用
for value in range(1,6):
呼叫函式**range()**時,也可指定列印一個引數,他將從0開始,
使用range()創建數字串列
要創建數字串列,可使用函式list()將range()的結果直接轉換為串列,如果將range()作為list()的一個引數,輸出的將時數字串列,
numbers = list(range(1,6)) print(numbers)
運行結果
[1, 2, 3, 4, 5]
使用range函式也可指定步長
numbers = list(range(1,6,2)) print(numbers)
運行結果
[1, 3, 5]
使用range()依然可以創建一些你想要的數集,例如得出在0——10中偶數的平方的數集
squares = [] for numbers in range(0,11,2): square = numbers ** 2 squares.append(square) print(squares)
運行結果
[0, 4, 16, 36, 64, 100]
切片
要創建切片,可指定要使用的第一個和最后一個元素的索引,與函式range()一樣python到達到達第二個索引前截至,
你可以生產串列的任意子集,如果想提取串列的第1,2,3個數,可將起始索引指定為0,終止索引指定為3,
如果起始沒有指定索引那么python將在開頭開始提取
如果終止沒有指定索引那么python將至結尾結束
value = https://www.cnblogs.com/hahaa/archive/2022/11/08/list(range(1,11)) print(value[0:3]) print(value[4:]) print(value[:4])
運行結果
[1, 2, 3] [5, 6, 7, 8, 9, 10] [1, 2, 3, 4]
元組
串列非常適合用于存盤在程式運行期間可能變化的資料集,串列時可以修改的,但有時候需要創建一系列不可修改的元素,元組可以滿足這種需求,
元組看起來很像串列,使用圓括號而不是中括號來標識,定義元組后,可以使用索引來訪問元素,就像訪問串列一樣,
下例操作一下元組及嘗試修改元組,
name = ('xiao','xiong') print(name[0]) print(name[1]) name[1]='old'
運行結果
Traceback (most recent call last): File "C:\Users\Desktop\python\py1.py", line 195, in <module> name[1]='old' TypeError: 'tuple' object does not support item assignment xiao xiong
由此可見,如果對元組內的資料進行更改,則會出現報錯,
按照串列訪問的方法可以正常訪問,
遍歷元組中的所有值
像串列一樣,可以使用for回圈來遍歷元組中的所有值
numbers = (1,2,3,4,5,6) for number in numbers: print(number)
#運行結果
1 2 3 4 5 6
修改元組變數
雖然不能修改元組的元素,但可以存盤元組的變數賦值,因此可以遍歷整個元組:
numbers = (1,2,3,4,5,6) print("original numbers") for number in numbers: print(number) numbers = (11,22,33,44,55,66) print("change numbers") for number in numbers: print(number)
運行結果
original numbers 1 2 3 4 5 6 change numbers 11 22 33 44**加粗樣式** 55 66
首先定義一個元組,并將其存盤的資料列印出來,接下來將一個新元組關聯到變數numbers,然后列印新的資料,這一次可以成功編譯,因為給元組變數重新賦值是合法的,
今天的分享就到這里結束了,祝各位早日學有所成!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529867.html
標籤:其他
下一篇:使用python繪制折線圖
