Python學(xué)習(xí)筆記基本數(shù)據(jù)結(jié)構(gòu)之序列類型list tuple range用法分析
本文實例講述了Python學(xué)習(xí)筆記基本數(shù)據(jù)結(jié)構(gòu)之序列類型list tuple range用法。分享給大家供大家參考,具體如下:
list 和 tuple
- list:列表,由 [] 標(biāo)識; 有序;可改變列表元素
- tuple:元組,由 () 標(biāo)識; 有序;不可改變元組元素(和list的主要區(qū)別)
list 和 tuple 的創(chuàng)建:
print([]) # 空list
print(["a",1,True]) # 元素類型不限
print([x for x in range(0,6)]) # 列表推導(dǎo)式
print(list("a"),type(list("a"))) # 強制轉(zhuǎn)化
print(()) # 空tuple
print((1)) # 不是tuple
print((1,)) # 單一元素tuple 一定要加,
print(("a",1,True)) # 元素類型不限
print(tuple("a"),type(tuple("a"))) # 強制轉(zhuǎn)化
空list l = []
list 用一對方括號,用','隔開里面的元素 l = [a] l = ["a",1,True] 元素類型不限
列表推導(dǎo)式,如:[x for x in range(0,6)] (下方會詳細介紹 range 及 列表推導(dǎo)式)
類型轉(zhuǎn)換 list()
空tuple t = ()
tuple 若只有一個元素時,注意表示為 t = (1,) 一定要有逗號
tuple 用一對圓括號,用','隔開里面多個的元素 t = ("a",1,True) 元素類型不限
類型轉(zhuǎn)換 tuple()
range
range 可方便的生成一個等差的序列,有兩種表示 range(stop) 、range(start, stop[, step]) ; 通常用在 for循環(huán)語句中
range(stop) 表示 0 到 stop(不包含stop) 等差為1 的數(shù),如 range(4) 表示 0 1 2 3
range(start, stop[, step]) 表示 從 start 到 stop(不包含stop) 等差為step的數(shù);step缺省為1,可設(shè)置為負數(shù)
print(type(range(4))) # range本身就是一個type for i in range(4): print(i) # 0 1 2 3 for i in range(-1): # 從0計數(shù),無值 print(i) for i in range(4,7): # 4 5 6 print(i) for i in range(2,7,2): # 2 4 6 print(i) for i in range(5,2,-1): # 5 4 3 print(i)
序列操作
一般操作,不改變list本身
| Operation | Result |
|---|---|
| x in s | True if an item of s is equal to x, else False |
| x not in s | False if an item of s is equal to x, else True |
| s + t | the concatenation of s and t |
| s * n or n * s | n shallow copies of s concatenated |
| s[i] | ith item of s, origin 0 |
| s[i:j] | slice of s from i to j |
| s[i:j:k] | slice of s from i to j with step k |
| len(s) | length of s |
| min(s) | smallest item of s |
| max(s) | largest item of s |
| s.index(x[, i[, j]]) | index of the first occurrence of x in s (at or after index i and before index j) |
| s.count(x) | total number of occurrences of x in s |
s = ["a",1,True,["b"],2]
print("a" in s) # 判斷元素存在于s
print("a" not in s) # 判斷元素不存在于s
print("b" in s)
print(1.0 in s) # 這邊不判斷int float類型不同
print("1" in s) # 這邊的1為字符串
a = [1,2]
b = [2,1,0]
print(a+b) # 序列相加
print(a*3) # 序列乘法
s = [0,1.0,2,3,4,5,6,7,8]
print(s[0],s[2],s[3]) # 通過下標(biāo)來取出對應(yīng)的元素
print(type(s[0]))
print(type(s[1]))
print(s[2:4]) # 取出一段list
print(s[2:7:2]) # 根據(jù)步長取出一段list
print(len(s)) # list長度,即包含幾個元素
sum = 0
for i in range(0,len(s)): # 使用for循環(huán)來取出list的每個元素
print(s[i])
sum += i # 賦值的簡單表達式,相當(dāng)于 sum = sum + i
print(sum) # 總和
print(min(s),max(s)) # 取最小/最大;注意元素類型間若不可比較,會報錯
s = [2,3,1,2,2,3]
print(s.index(2)) # 查找對應(yīng)元素第一次出現(xiàn)的下標(biāo)
# print(s.index(4)) # 不存在該元素會報錯
print(s.index(2,3)) # 從下標(biāo)為3的開始找起
print(s.index(2,3,4)) # 從下標(biāo)為3到下標(biāo)4的階段內(nèi)找
print(s.count(2)) # 輸出為2的元素的個數(shù)
print(s.count("2")) # 找不到匹配元素,返回0
上方列出的操作方法對 tuple 也都適用,因為并不改變序列本身的元素,如
s = (2,3,1,2,2,3) print(s[2],s[2:4],len(s),s.count(2)) # 對tuple均適用
改變序列的操作:僅對 list 適用;若對 tuple 操作,會報錯;clear() 和 copy() 是 Python 3.3 才新增的方法
| Operation | Result |
|---|---|
| s[i] = x | item i of s is replaced by x |
| s[i:j] = t | slice of s from i to j is replaced by the contents of the iterable t |
| s[i:j:k] = t | the elements of s[i:j:k] are replaced by those of t |
| del s[i:j] | same as s[i:j] = [] |
| del s[i:j:k] | removes the elements of s[i:j:k] from the list |
| s.pop([i]) | retrieves the item at i and also removes it from s |
| s.remove(x) | remove the first item from s where s[i] == x |
| s.clear() | removes all items from s (same as del s[:]) |
| s.append(x) | appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) |
| s.extend(t) | extends s with the contents of t (same as s[len(s):len(s)] = t) |
| s.insert(i, x) | inserts x into s at the index given by i (same as s[i:i] = [x]) |
| s.copy() | creates a shallow copy of s (same as s[:]) |
| s.reverse() | reverses the items of s in place |
list的增、刪、改的操作實際都比較實用,需要熟練掌握
list元素更改
可對 list 不同的下標(biāo)表示法做以下操作,一般 list 下標(biāo)的操作僅作對單一元素的更改賦值,如 s[0]=1 ;對多個元素的操作見下方示例(僅供參考)
s = [0,1,2,3] s[0] = "1" print(s) # 對list的某一元素賦另外的值,類型也跟隨改變 s[4] = 1 # 不可超過原list的長度,會報錯 s[0:3] = [2,3,4] # 可對一段元素賦另外的值 print(s) s[0:3] = ["x","x"] # 可缺少,元素個數(shù)也就相應(yīng)的減少了 print(s) s[0:2] = ["x","x","x","x"] # 可增加,元素個數(shù)也就相應(yīng)的減加了 print(s) s[0] = [0,0] # 單個元素注意,相當(dāng)于賦值,把序列賦予該元素 print(s) s[1:2] = [0,0] print(s) s = [0,1,2,3,4,5,6,7,8] s[1:8:2] = ["x"]*4 # s[1:8:2] = ["x"]*3 # 這種表示方式元素個數(shù)一定需要相同,不然會報錯 print(s)
list元素刪除
s = [0,1,2,3,4,5,6,7,8] del s[0:4] # 刪除對應(yīng)的元素 print(s) s = [0,1,2,3,4,5,6,7,8] del s[1:8:2] # 做刪除 print(s) s = [0,1,2,3,4,5,6,7,8] s.pop(3) print(s.pop(3),s) # 做刪除,并且返回該元素的值 print(s.pop(),s) # 默認刪除最后一個 s = [2,"1",1.0,1,2,1] s.remove(1) # 刪除第一個值為 1 的元素 print(s) s.clear() # 置空,Python3.3引入 print(s)
list元素增加
s = [0,1,2,3,4] s.append(5) # list 最后加一個元素 print(s) s.extend([6,7]) # list 最后拼接序列 print(s) s.extend(range(3)) print(s) s.insert(1,["x"]) # 在1的位置插入["x"] print(s)
其他操作,reverse、copy 等
s = [1,2,3] c = s.copy() # 相當(dāng)于 c = s print(c) c.reverse() print(c) s = [2,3,1,4] s.sort() # 排序 print(s) # s = ["b",1,"a",True] # 報錯,必須是可比較的類型 s = ["b","a"] s.sort() print(s)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python中實現(xiàn) xls 文件轉(zhuǎn) xlsx的4種方法(示例詳解)
在 Python 中,可以采用 pandas、pyexcel、win32com 和 xls2xlsx 這四個模塊,實現(xiàn) xls 轉(zhuǎn) xlsx 格式,本文以 Excel 示例文件test_Excel.xls 為例結(jié)合示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-06-06
使用Python開發(fā)一個簡單的本地圖片服務(wù)器
本文介紹了如何結(jié)合wxPython構(gòu)建的圖形用戶界面GUI和Python內(nèi)建的 Web服務(wù)器功能,在本地網(wǎng)絡(luò)中搭建一個私人的,即開即用的網(wǎng)頁相冊,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2025-04-04
Python HTMLTestRunner可視化報告實現(xiàn)過程解析
這篇文章主要介紹了Python HTMLTestRunner可視化報告實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Python報錯ValueError: cannot reindex from
當(dāng)處理Pandas數(shù)據(jù)框(DataFrame)時,你是否遇到過ValueError: cannot reindex from a duplicate axis的報錯?這個問題通常發(fā)生在嘗試對DataFrame進行重索引時,如果索引有重復(fù)值,就會觸發(fā)這個錯誤,下面,我們將探討這個問題并提供解決方法2024-09-09

