list串列
增加
- append 在串列中追加,一次只能加一個
- insert 按索引插入,一次只能插一個
- extend 迭代追加到串列中
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.append(2)
print(list1)
list1.insert(1, 4)
print(list1)
list1.extend(list2)
print(list1)
輸出:
[1, 2, 3, 2]
[1, 4, 2, 3, 2]
[1, 4, 2, 3, 2, 4, 5, 6]
洗掉
- remove 按照元素值洗掉,一次只能洗掉一個
- pop 按索引洗掉,也可以按切片跟步長洗掉,默認洗掉最后一個(輸出回傳值)
- clear 清空串列
- del 洗掉整個串列
list1 = [1, 2, 3, 4, 5, 6]
list1.remove(3)
print(list1)
list1.pop()
print(list1)
list1.pop(2) #或者del list1[2]不推薦
print(list1)
list1.clear()
print(list1)
del list1
print(list1)
輸出:
[1, 2, 4, 5, 6]
[1, 2, 4, 5]
[1, 2, 5]
[]
NameError: name 'list1' is not defined
修改
- 按索引去改
- 按切片步長去改
list1 = [1,2,3,4,5,6]
list1[2] = 9
print(list1)
list1[1:4] = [11,12,13]
print(list1)
輸出:
[1, 2, 9, 4, 5, 6]
[1, 11, 12, 13, 5, 6]
查詢
- 按索引,切片加步長去查
- 用for回圈查
list1 = [1,2,3,4,5,6]
list2 = list1[0:5:2]
print(list2)
for i in list1:
print(i)
輸出:
[1, 3, 5]
1
2
3
4
5
6
其他方法
- len() 查詢串列長度
- count 統計某個元素出現的個數
- index 通過元素找索引
- sort 排序串列,括號里接reverse=True就是從大到小
- reverse 倒敘串列
- enumerate 用for回圈,給序列加序號
list1=[1,2,3,4,5,6,7,4,5,4]
print(len(list1))
print(list1.count(4))
print(list1.index(2))
print(list1.index(4)) #默認顯示第一個匹配的元素的索引
list1.sort(reverse=True)
print("降序:", list1
for index,value in enumerate(list1):
print("{0},{1}".format(index,value))
輸出:
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,1
tuple元組
tuple元組與串列類似,不同之處在于元組的元素不能修改
查詢
按索引去查
按切片步長去查
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
輸出:
tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)
修改
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz') #tup1[0] = 100 該方法是非法的
tup3 = tup1 + tup2; #創建一個新的元組
print (tup3)
輸出:
(12, 34.56, 'abc', 'xyz')
洗掉
元組中的元素值是不允許洗掉的,但我們可以使用del陳述句來洗掉整個元組
串列和元祖的轉換(如想要保護資料,可把list轉換成元祖,如修改可轉成串列)
'''
遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書!
'''
tup = ('Google', 'Runoob', 1997, 2000)
print (tup)
del tup;
print ("洗掉后的元組 tup : ")
print (tup)
輸出:
NameError: name 'tup' is not defined
dict字典
增加
正常方式寫入即可,有則不動,無則增加
dict1 = {"name": "小明", "age": 18, "height": 1.75}
dict1["wegit"] = 64.5
print(dict1)
輸出:
{'name': '小明', 'age': 18, 'height': 1.75, 'wegit': 64.5}
修改
update 把另一個鍵值隊合并到一個,相同的覆寫,沒有的添加
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 洗掉最后一個
clear 清空
del 洗掉key或全部洗掉
dict1 = {'name': '小華', 'age': 18, 'height': 1.75, 'wegit': 64.5, 'curriculum': 'English'}
dict1.popitem()
print(dict1)
dict1.pop("wegit")
print(dict1)
del dict1["name"]
print(dict1)
dict1.clear()
print()
輸出:
{'name': '小華', 'age': 18, 'height': 1.75, 'wegit': 64.5}
{'name': '小華', 'age': 18, 'height': 1.75}
{'age': 18, 'height': 1.75}
{}
查詢
對鍵遍歷
對鍵和值遍歷
d = {'name1' : 'pythontab', 'name2' : '.', 'name3' : 'com'}
for key in d:
print (key, ' value : ', d[key])
輸出:
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 : com
set集合
增加
add 添加一個字串
update 迭代著添加
b = {'b', 'c'} #定義集合
print(b)
a = set('boy') #定義集合
print(a)
a.add('python')
print(a)
a.update('update')
print(a)
輸出:
{'b', 'c'}
{'b', 'y', 'o'}
{'b', 'python', 'y', 'o'}
{'b', 'a', 'python', 'u', 'd', 't', 'e', 'p', 'o', 'y'}
洗掉
remove 按元素洗掉
discard 集合的刪,跟remove刪是一樣的,沒有不會報
pop 隨機洗掉一個元素,有回傳值
clear 清空集合
del 洗掉整個集合
a.remove('python')
print(a)
a.discard('python') #已經洗掉python元素了,再用discard洗掉不會報錯
print(a)
a.pop()
print(a)
a.clear()
print(a)
del a
print(a)
輸出:
{'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】
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】
回傳兩個集合中不重復的元素
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
True
frozenset 不可變集合
a = frozenset("abcd")
print(a)
a.add("d")
報錯:AttributeError: 'frozenset' object has no attribute 'add'
輸出:
frozenset({'b', 'a', 'd', 'c'})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161031.html
標籤:Python
上一篇:python中有數學功能的函式
下一篇:Python第十三章-網路編程
