Python列表元組字典集合存儲結構詳解
1 列表
python最重要的就是操作數據結構,所以要知道數據使用才行,一般常用的就是數據類型的相互嵌套,要清晰把我是什么數據結構。
列表就是['數據1', '數據2', '數據3', '數據4'.....],元組就是不可變的列表t1 = (10, 20, 30),但要注意t3 = (20)這個還是int類型,t3 = (20,)這樣才是元組,字典就是dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'},集合就是set,s2 = {10, 30, 20, 10, 30, 40, 30, 50}會對元素去重
1.1 列表語法
['數據1', '數據2', '數據3', '數據4'.....]
1.2 列表的常用操作
列表的作?是?次性存儲多個數據,程序員可以對這些數據進?的操作有:增、刪、改、查
1.2.1 查找
- 下標查找
name_list = ['oldlu', 'xiaofang', 'xiaolu'] print(name_list[0]) print(name_list[1]) print(name_list[2])
具體示例如下:
- 函數方法
index():返回指定數據所在位置的下標 。
語法
列表序列.index(數據, 開始位置下標, 結束位置下標)
快速體驗
name_list =['oldlu', 'xiaofang', 'xiaolu'] print(name_list.index('oldlu', 0, 2))
具體示例如下:
注意:如果查找的數據不存在則報錯。
count():統(tǒng)計指定數據在當前列表中出現(xiàn)的次數。
name_list = ['oldlu', 'xiaofang', 'xiaolu'] print(name_list.count('xiaofang'))
具體示例如下:
len():訪問列表?度,即列表中數據的個數。
name_list = ['oldlu', 'xiaofang', 'xiaolu'] print(len(name_list)) #3
- 判斷是否存在
in:判斷指定數據在某個列表序列,如果在返回True,否則返回False
name_list = ['wushen', 'xiaoyuan', 'cheng'] # 結果:True print('wushen' in name_list) # 結果:False print('wushens' in name_list)
具體示例如下:
not in:判斷指定數據不在某個列表序列,如果不在返回True,否則返回False
name_list = ['wushen', 'xioayuan', 'cheng'] # 結果:False print('wushen' not in name_list) # 結果:True print('wushens' not in name_list)
具體示例如下:
1.2.2 增加
作?:增加指定數據到列表中。
1.語法
append():列表結尾追加數據。
列表序列.append(數據)
2. 體驗
name_list = ['wushen', 'xiaoyuan', 'cheng'] name_list.append('xingdie') # 結果:['wushen', 'xiaoyuan', 'cheng', 'xingdie'] print(name_list)
列表追加數據的時候,直接在原列表??追加了指定數據,即修改了原列表,故列表為可變類型 數據。
3. 注意點
如果append()追加的數據是?個序列,則追加整個序列到列表
name_list = ['wushen', 'xiaoyuan', 'cheng'] name_list.append(['xingdie', 'xingdie']) # 結果:['wushen', 'xiaoyuan', 'cheng', ['xingdie', 'xingdie']] print(name_list)
extend():列表結尾追加數據,如果數據是?個序列,則將這個序列的數據逐?添加到列表。
1. 語法
列表序列.extend(數據)
2. 快速體驗
單個數據
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.extend('liujia') # 結果:['wushen', 'xiaoyuan', 'xingdie', 'l', 'i', 'u', 'j', 'i', 'a'] print(name_list)
具體示例如下:
序列數據
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.extend(['liujia', 'zhihong']) # 結果:['wushen', 'xiaoyuan', 'xingdie', 'liujia', 'zhihong'] print(name_list)
具體示例如下:
insert():指定位置新增數據。
1. 語法
列表序列.insert(位置下標, 數據)
2. 快速體驗
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.insert(1, 'liujia') # 結果:['wushen', 'liujia', 'xiaoyuan', 'xingdie'] print(name_list)
具體示例如下:
1.2.3 刪除
• del
1. 語法
del ?標
2. 快速體驗
刪除列表
name_list = ['wushen', 'xiaoyuan', 'xingdie'] # 結果:報錯提示:name 'name_list' is not defined del name_list print(name_list)
具體示例如下:
刪除指定數據
name_list = ['wushen', 'xiaoyuan', 'xingdie'] del name_list[0] # 結果:['xiaoyuan', 'xingdie'] print(name_list)
具體示例如下:
pop():刪除指定下標的數據(默認為最后?個),并返回該數據。
1. 語法
列表序列.pop(下標)
2. 快速體驗
name_list = ['wushen', 'xiaoyuan', 'xingdie'] del_name = name_list.pop(1) # 結果:xiaoyuan print(del_name) # 結果:['wushen', 'xingdie'] print(name_list)
具體示例如下:
remove():移除列表中某個數據的第?個匹配項。
1. 語法
1 列表序列.remove(數據)
2. 快速體驗
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.remove('xingdie') # 結果:['wushe', 'xiaoyuan'] print(name_list)
clear():清空列表
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.clear() print(name_list) # 結果: []
具體示例如下:
1.2.4 修改
修改指定下標數據
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list[0] = 'ccc' # 結果:['ccc', 'xiaoyaun', 'xingdie'] print(name_list)
逆置:reverse()
num_list = [1, 5, 2, 3, 6, 8] num_list.reverse() # 結果:[8, 6, 3, 2, 5, 1] print(num_list)
具體示例如下:
排序:sort()
1. 語法
列表序列.sort( key=None, reverse=False)
注意:reverse表示排序規(guī)則,reverse = True 降序, reverse = False 升序(默認)
2. 快速體驗
num_list = [1, 5, 2, 3, 6, 8] num_list.sort() # 結果:[1, 2, 3, 5, 6, 8] print(num_list)
1.2.6 復制
函數:copy()
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_li2 = name_list.copy() # 結果:['wushen', 'xiaoyuan', 'xingdie'] print(name_li2)
具體示例如下:
1.3 列表的循環(huán)遍歷
1.3.1 while
•代碼
name_list = ['wushen', 'xiaoyuan', 'xingdie'] i = 0 while i < len(name_list): print(name_list[i]) i += 1
具體示例如下:
1.3.2 for
代碼
name_list = ['wushen', 'xiaoyuan', 'xingdie'] for i in name_list: print(i)
具體示例如下:
1.4 列表嵌套
所謂列表嵌套指的就是?個列表??包含了其他的?列表。
name_list = [['吳神', '?園', '星蝶'], ['wushen', 'xiaoyuan', 'xingxie'], ['張三', '李四', '王五']]
思考: 如何查找到數據"李四"?
# 第?步:按下標查找到李四所在的列表 print(name_list[2]) # 第?步:從李四所在的列表??,再按下標找到數據李四 print(name_list[2][1])
具體示例如下:
2 元組
2.1 定義元組
思考:如果想要存儲多個數據,但是這些數據是不能修改的數據,怎么做?
答:列表?列表可以?次性存儲多個數據,但是列表中的數據允許更改。
?個元組可以存儲多個數據,元組內的數據是不能修改的。
元組特點:定義元組使??括號,且逗號隔開各個數據,數據可以是不同的數據類型
# 多個數據元組 t1 = (10, 20, 30) # 單個數據元組 t2 = (10,)
注意:如果定義的元組只有?個數據,那么這個數據后?也好添加逗號,否則數據類型為唯?的 這個數據的數據類型
t2 = (10,) print(type(t2)) # tuple t3 = (20) print(type(t3)) # int t4 = ('hello') print(type(t4)) # str
具體示例如下:
2.2 元組的常見操作
元組數據不?持修改,只?持查找,具體如下:
- 按下標查找數據
tuple1 = ('aa', 'bb', 'cc', 'bb') print(tuple1[0]) # aa
- index():查找某個數據,如果數據存在返回對應的下標,否則報錯,語法和列表、字符串的index ?法相同。
tuple1 = ('aa', 'bb', 'cc', 'bb') print(tuple1.index('aa')) # 0
- count():統(tǒng)計某個數據在當前元組出現(xiàn)的次數。
tuple1 = ('aa', 'bb', 'cc', 'bb') print(tuple1.count('bb')) # 2
- len():統(tǒng)計元組中數據的個數。
tuple1 = ('aa', 'bb', 'cc', 'bb') print(len(tuple1)) # 4
注意:元組內的直接數據如果修改則?即報錯
tuple1 = ('aa', 'bb', 'cc', 'bb') tuple1[0] = 'aaa'
但是如果元組??有列表,修改列表??的數據則是?持的,故覺很重要。
tuple2 = (10, 20, ['aa', 'bb', 'cc'], 50, 30) print(tuple2[2]) # 訪問到列表 # 結果:(10, 20, ['aaaaa', 'bb', 'cc'], 50, 30) tuple2[2][0] = 'aaaaa' print(tuple2)
3 字典
3.1 創(chuàng)建字典的語法
字典特點:
- 符號為?括號
- 數據為鍵值對形式出現(xiàn)
- 各個鍵值對之間?逗號隔開
# 有數據字典 dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} # 空字典 dict2 = {} dict3 = dict()
注意:?般稱冒號前?的為鍵(key),簡稱k;冒號后?的為值(value),簡稱v。
3.2 字典常見操作
3.2.1 增加
寫法:字典序列[key] = 值
注意:如果key存在則修改這個key對應的值;如果key不存在則新增此鍵值對。
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} dict1['name'] = 'wushen' # 結果:{'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1) dict1['id'] = 110 dict1['name'] = 110
具體示例如下:
注意:字典為可變類型。
3.2.2 刪除
del() / del:刪除字典或刪除字典中指定鍵值對
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} del dict1['gender'] # 結果:{'name': 'wushen', 'age': 19} print(dict1)
具體示例如下:
clear():清空字典
dict1 = {'name': 'wushne', 'age': 19, 'gender': '男'} dict1.clear() print(dict1) # {}
具體示例如下:
3.2.3 修改
寫法:字典序列[key] = 值
注意:如果key存在則修改這個key對應的值 ;如果key不存在則新增此鍵值對。
3.2.4 查詢
key值查找
dict1 ={'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1['name']) print(dict1['id']) # 報錯
具體示例如下:
如果當前查找的key存在,則返回對應的值;否則則報錯。
get()
- 語法
字典序列.get(key, 默認值)
注意:如果當前查找的key不存在則返回第?個參數(默認值),如果省略第?個參數,則返回 None。
- 快速體驗
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1.get('name')) # wushen print(dict1.get('id', 110)) # 110 print(dict1.get('id')) # None print(dict1.get('name',110)) # wushen
具體示例如下:
keys()獲得所有的鍵
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1.keys()) # dict_keys(['name', 'age', 'gender'])
具體示例如下:
values()獲得所有的值
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1.values()) # dict_values(['wushen', 19, '男'])
items()獲得所有的鍵值對
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} print(dict1.items()) # dict_items([('name', 'wushen'), ('age', 19), ('gender','男')])
3.3 字典的循環(huán)遍歷
3.3.1 遍歷字典的key
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} for key in dict1.keys(): print(key)
3.3.2 遍歷字典的value
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} for value in dict1.values(): print(value)
3.3.3 遍歷字典的元素
dict1={'name':'wushen','age':19,'gender':'男'} for item in dict1.items(): print(item)
具體示例如下:
3.3.4 遍歷字典的鍵值對
dict1 = {'name': 'wushen', 'age': 19, 'gender': '男'} for key, value in dict1.items(): print(f'{key} = {value}')
4 集合
4.1 創(chuàng)建集合
創(chuàng)建集合使用 {} 或 set() , 但是如果要創(chuàng)建空集合只能使用 set() ,因為 {} 用來創(chuàng)建空字典。
特點: 1. 集合可以去掉重復數據;2. 集合數據是?序的,故不?持下標。
s1 = {10, 20, 30, 40, 50} print(s1) s2 = {10, 30, 20, 10, 30, 40, 30, 50} print(s2) s3 = set('abcdefg') print(s3) s4 = set() print(type(s4)) # set s5 = {} print(type(s5)) # dict
具體示例如下:
4.2 集合常見操作方法
4.2.1 增加數據
add()
s1 = {10, 20} s1.add(100) s1.add(10) print(s1) # {100, 10, 20}
因為集合有去重功能,所以,當向集合內追加的數據是當前集合已有數據的話,則不進?任何操作。
update()
,追加的數據是序列。
s1 = {10, 20} # s1.update(100) # 報錯 s1.update([100, 200]) s1.update('abc') print(s1)
4.2.2 刪除數據
remove()
刪除集合中的指定數據,如果數據不存在則報錯。
s1 = {10, 20} s1.remove(10) print(s1) s1.remove(10) # 報錯 print(s1)
discard()
刪除集合中的指定數據,如果數據不存在也不會報錯。
s1 = {10, 20} s1.discard(10) print(s1) s1.discard(10) # 報錯 print(s1)
pop()
隨機刪除集合中的某個數據,并返回這個數據。
s1 = {10, 20, 30, 40, 50} del_num = s1.pop() print(del_num) print(s1)
4.2.3 查找數據
in
:判斷數據在集合序列not in
:判斷數據不在集合序列
s1 = {10, 20, 30, 40, 50} print(10 in s1) print(10 not in s1)
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python使用urllib模塊和pyquery實現(xiàn)阿里巴巴排名查詢
這篇文章主要介紹了python庫urllib及pyquery基本東西的應用,實現(xiàn)阿里巴巴關鍵詞排名的查詢,其中涉及到urllib代理的設置,pyquery對html文檔的解析2014-01-01python實現(xiàn)生成Word、docx文件的方法分析
這篇文章主要介紹了python實現(xiàn)生成Word、docx文件的方法,結合實例形式分析了Python使用docx模塊操作word文件與docx文件的相關實現(xiàn)技巧,需要的朋友可以參考下2019-08-08