Python list,tuple,dict,set高级变量常用方法
list列表
增加
輸出:
[1, 2, 3, 2] [1, 4, 2, 3, 2] [1, 4, 2, 3, 2, 4, 5, 6]刪除
輸出:
[1, 2, 4, 5, 6] [1, 2, 4, 5] [1, 2, 5] [] NameError: name 'list1' is not defined修改
輸出:
[1, 2, 9, 4, 5, 6] [1, 11, 12, 13, 5, 6]查詢(xún)
輸出:
[1, 3, 5] 1 2 3 4 5 6其他方法
輸出:
10 3 1 3 降序: [7, 6, 5, 5, 4, 4, 4, 3, 2, 1] 0,7 1,6 2,5 3,5 4,4 5,4 6,4 7,3 8,2 9,1tuple元組
tuple元組與列表類(lèi)似,不同之處在于元組的元素不能修改
查詢(xún)
按索引去查
按切片步長(zhǎng)去查
輸出:
tup1[0]: Google tup2[1:5]: (2, 3, 4, 5)修改
tup1 = (12, 34.56); tup2 = ('abc', 'xyz') #tup1[0] = 100 該方法是非法的 tup3 = tup1 + tup2; #創(chuàng)建一個(gè)新的元組 print (tup3)輸出:
(12, 34.56, 'abc', 'xyz')刪除
元組中的元素值是不允許刪除的,但我們可以使用del語(yǔ)句來(lái)刪除整個(gè)元組
列表和元祖的轉(zhuǎn)換(如想要保護(hù)數(shù)據(jù),可把list轉(zhuǎn)換成元祖,如修改可轉(zhuǎn)成列表)
輸出:
NameError: name 'tup' is not defineddict字典
增加
正常方式寫(xiě)入即可,有則不動(dòng),無(wú)則增加
dict1 = {"name": "小明", "age": 18, "height": 1.75} dict1["wegit"] = 64.5 print(dict1)輸出:
{'name': '小明', 'age': 18, 'height': 1.75, 'wegit': 64.5}修改
update 把另一個(gè)鍵值隊(duì)合并到一個(gè),相同的覆蓋,沒(méi)有的添加
''' 遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)! ''' dict2 = {"name": "小華", "age": 18, "curriculum": "English"} dict1.update(dict2) print(dict1)輸出:
{'name': '小華', 'age': 18, 'height': 1.75, 'wegit': 64.5, 'curriculum': 'English'}刪除
pop 按key刪
popitem 刪除最后一個(gè)
clear 清空
del 刪除key或全部刪除
輸出:
{'name': '小華', 'age': 18, 'height': 1.75, 'wegit': 64.5} {'name': '小華', 'age': 18, 'height': 1.75} {'age': 18, 'height': 1.75} {}查詢(xún)
對(duì)鍵遍歷
對(duì)鍵和值遍歷
輸出:
name1 value : pythontab name2 value : . name3 value : com for key, value in d.items():print (key, ' value : ', value)輸出:
name1 value : pythontab name2 value : . name3 value : comset集合
增加
add 添加一個(gè)字符串
update 迭代著添加
輸出:
{'b', 'c'} {'b', 'y', 'o'} {'b', 'python', 'y', 'o'} {'b', 'a', 'python', 'u', 'd', 't', 'e', 'p', 'o', 'y'}刪除
remove 按元素刪除
discard 集合的刪,跟remove刪是一樣的,沒(méi)有不會(huì)報(bào)
pop 隨機(jī)刪除一個(gè)元素,有返回值
clear 清空集合
del 刪除整個(gè)集合
輸出:
{'b', 'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'} {'b', 'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'} {'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'} set() NameError: name 'a' is not defined交集【& or intersection】
''' 遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)! ''' x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} z = x.intersection(y) print(z) c = x & y print(c)輸出:
{'apple'} {'apple'}并集【| or union】
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} z = x.union(y) print(z) c = x | y print(c)輸出:
{'cherry', 'runoob', 'google', 'banana', 'apple'} {'cherry', 'runoob', 'google', 'banana', 'apple'}差集【- or difference】
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.difference(y) print(z) z1 = y.difference(x) print(z1) c = x - y print(c) c1 = y - x print(c1)輸出:
{'cherry', 'banana'} {'google', 'microsoft'} {'cherry', 'banana'} {'google', 'microsoft'}反交集【^ or symmetric_difference】
返回兩個(gè)集合中不重復(fù)的元素
''' 遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)! ''' x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.symmetric_difference(y) print(z) m = x ^ y print(m)輸出:
{'microsoft', 'google', 'cherry', 'banana'} {'microsoft', 'google', 'cherry', 'banana'}子集【< or issubset】
a = {1,2} b = {1,2,3,4} print(a.issubset(b)) #a是b的子集 print(a < b)輸出:
True True超集【> or issuperset】
a = {1,2} b = {1,2,3,4} print(b.issuperset(a)) #b是a的超集 print(b > a)輸出:
True Truefrozenset 不可變集合
a = frozenset("abcd") print(a) a.add("d")報(bào)錯(cuò):AttributeError: ‘frozenset’ object has no attribute ‘a(chǎn)dd’
輸出:
frozenset({'b', 'a', 'd', 'c'})總結(jié)
以上是生活随笔為你收集整理的Python list,tuple,dict,set高级变量常用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python递归方式和普通方式实现输出和
- 下一篇: python挖坑法实现快排