Python列表元組字典集合存儲結構詳解
1 列表
python最重要的就是操作數(shù)據(jù)結構,所以要知道數(shù)據(jù)使用才行,一般常用的就是數(shù)據(jù)類型的相互嵌套,要清晰把我是什么數(shù)據(jù)結構。
列表就是['數(shù)據(jù)1', '數(shù)據(jù)2', '數(shù)據(jù)3', '數(shù)據(jù)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 列表語法
['數(shù)據(jù)1', '數(shù)據(jù)2', '數(shù)據(jù)3', '數(shù)據(jù)4'.....]
1.2 列表的常用操作
列表的作?是?次性存儲多個數(shù)據(jù),程序員可以對這些數(shù)據(jù)進?的操作有:增、刪、改、查
1.2.1 查找
- 下標查找
name_list = ['oldlu', 'xiaofang', 'xiaolu'] print(name_list[0]) print(name_list[1]) print(name_list[2])
具體示例如下:

- 函數(shù)方法
index():返回指定數(shù)據(jù)所在位置的下標 。
語法
列表序列.index(數(shù)據(jù), 開始位置下標, 結束位置下標)
快速體驗
name_list =['oldlu', 'xiaofang', 'xiaolu']
print(name_list.index('oldlu', 0, 2))具體示例如下:

注意:如果查找的數(shù)據(jù)不存在則報錯。
count():統(tǒng)計指定數(shù)據(jù)在當前列表中出現(xiàn)的次數(shù)。
name_list = ['oldlu', 'xiaofang', 'xiaolu']
print(name_list.count('xiaofang'))具體示例如下:

len():訪問列表?度,即列表中數(shù)據(jù)的個數(shù)。
name_list = ['oldlu', 'xiaofang', 'xiaolu'] print(len(name_list)) #3
- 判斷是否存在
in:判斷指定數(shù)據(jù)在某個列表序列,如果在返回True,否則返回False
name_list = ['wushen', 'xiaoyuan', 'cheng']
# 結果:True
print('wushen' in name_list)
# 結果:False
print('wushens' in name_list)具體示例如下:
not in:判斷指定數(shù)據(jù)不在某個列表序列,如果不在返回True,否則返回False
name_list = ['wushen', 'xioayuan', 'cheng']
# 結果:False
print('wushen' not in name_list)
# 結果:True
print('wushens' not in name_list)具體示例如下:

1.2.2 增加
作?:增加指定數(shù)據(jù)到列表中。
1.語法
append():列表結尾追加數(shù)據(jù)。
列表序列.append(數(shù)據(jù))
2. 體驗
name_list = ['wushen', 'xiaoyuan', 'cheng']
name_list.append('xingdie')
# 結果:['wushen', 'xiaoyuan', 'cheng', 'xingdie']
print(name_list)列表追加數(shù)據(jù)的時候,直接在原列表??追加了指定數(shù)據(jù),即修改了原列表,故列表為可變類型 數(shù)據(jù)。
3. 注意點
如果append()追加的數(shù)據(jù)是?個序列,則追加整個序列到列表
name_list = ['wushen', 'xiaoyuan', 'cheng'] name_list.append(['xingdie', 'xingdie']) # 結果:['wushen', 'xiaoyuan', 'cheng', ['xingdie', 'xingdie']] print(name_list)
extend():列表結尾追加數(shù)據(jù),如果數(shù)據(jù)是?個序列,則將這個序列的數(shù)據(jù)逐?添加到列表。
1. 語法
列表序列.extend(數(shù)據(jù))
2. 快速體驗
單個數(shù)據(jù)
name_list = ['wushen', 'xiaoyuan', 'xingdie']
name_list.extend('liujia')
# 結果:['wushen', 'xiaoyuan', 'xingdie', 'l', 'i', 'u', 'j', 'i', 'a']
print(name_list)具體示例如下:

序列數(shù)據(jù)
name_list = ['wushen', 'xiaoyuan', 'xingdie'] name_list.extend(['liujia', 'zhihong']) # 結果:['wushen', 'xiaoyuan', 'xingdie', 'liujia', 'zhihong'] print(name_list)
具體示例如下:

insert():指定位置新增數(shù)據(jù)。
1. 語法
列表序列.insert(位置下標, 數(shù)據(jù))
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)
具體示例如下:

刪除指定數(shù)據(jù)
name_list = ['wushen', 'xiaoyuan', 'xingdie'] del name_list[0] # 結果:['xiaoyuan', 'xingdie'] print(name_list)
具體示例如下:

pop():刪除指定下標的數(shù)據(jù)(默認為最后?個),并返回該數(shù)據(jù)。
1. 語法
列表序列.pop(下標)
2. 快速體驗
name_list = ['wushen', 'xiaoyuan', 'xingdie'] del_name = name_list.pop(1) # 結果:xiaoyuan print(del_name) # 結果:['wushen', 'xingdie'] print(name_list)
具體示例如下:

remove():移除列表中某個數(shù)據(jù)的第?個匹配項。
1. 語法
1 列表序列.remove(數(shù)據(jù))
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 修改
修改指定下標數(shù)據(jù)
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 復制
函數(shù):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'], ['張三', '李四', '王五']]
思考: 如何查找到數(shù)據(jù)"李四"?
# 第?步:按下標查找到李四所在的列表 print(name_list[2]) # 第?步:從李四所在的列表??,再按下標找到數(shù)據(jù)李四 print(name_list[2][1])
具體示例如下:

2 元組
2.1 定義元組
思考:如果想要存儲多個數(shù)據(jù),但是這些數(shù)據(jù)是不能修改的數(shù)據(jù),怎么做?
答:列表?列表可以?次性存儲多個數(shù)據(jù),但是列表中的數(shù)據(jù)允許更改。
?個元組可以存儲多個數(shù)據(jù),元組內(nèi)的數(shù)據(jù)是不能修改的。
元組特點:定義元組使??括號,且逗號隔開各個數(shù)據(jù),數(shù)據(jù)可以是不同的數(shù)據(jù)類型
# 多個數(shù)據(jù)元組 t1 = (10, 20, 30) # 單個數(shù)據(jù)元組 t2 = (10,)
注意:如果定義的元組只有?個數(shù)據(jù),那么這個數(shù)據(jù)后?也好添加逗號,否則數(shù)據(jù)類型為唯?的 這個數(shù)據(jù)的數(shù)據(jù)類型
t2 = (10,)
print(type(t2)) # tuple
t3 = (20)
print(type(t3)) # int
t4 = ('hello')
print(type(t4)) # str具體示例如下:

2.2 元組的常見操作
元組數(shù)據(jù)不?持修改,只?持查找,具體如下:
- 按下標查找數(shù)據(jù)
tuple1 = ('aa', 'bb', 'cc', 'bb')
print(tuple1[0]) # aa- index():查找某個數(shù)據(jù),如果數(shù)據(jù)存在返回對應的下標,否則報錯,語法和列表、字符串的index ?法相同。
tuple1 = ('aa', 'bb', 'cc', 'bb')
print(tuple1.index('aa')) # 0- count():統(tǒng)計某個數(shù)據(jù)在當前元組出現(xiàn)的次數(shù)。
tuple1 = ('aa', 'bb', 'cc', 'bb')
print(tuple1.count('bb')) # 2- len():統(tǒng)計元組中數(shù)據(jù)的個數(shù)。
tuple1 = ('aa', 'bb', 'cc', 'bb')
print(len(tuple1)) # 4注意:元組內(nèi)的直接數(shù)據(jù)如果修改則?即報錯
tuple1 = ('aa', 'bb', 'cc', 'bb')
tuple1[0] = 'aaa'但是如果元組??有列表,修改列表??的數(shù)據(jù)則是?持的,故覺很重要。
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)建字典的語法
字典特點:
- 符號為?括號
- 數(shù)據(jù)為鍵值對形式出現(xiàn)
- 各個鍵值對之間?逗號隔開
# 有數(shù)據(jù)字典
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不存在則返回第?個參數(shù)(默認值),如果省略第?個參數(shù),則返回 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. 集合可以去掉重復數(shù)據(jù);2. 集合數(shù)據(jù)是?序的,故不?持下標。
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 增加數(shù)據(jù)
add()
s1 = {10, 20}
s1.add(100)
s1.add(10)
print(s1) # {100, 10, 20}
因為集合有去重功能,所以,當向集合內(nèi)追加的數(shù)據(jù)是當前集合已有數(shù)據(jù)的話,則不進?任何操作。
update(),追加的數(shù)據(jù)是序列。
s1 = {10, 20}
# s1.update(100) # 報錯
s1.update([100, 200])
s1.update('abc')
print(s1)4.2.2 刪除數(shù)據(jù)
remove()刪除集合中的指定數(shù)據(jù),如果數(shù)據(jù)不存在則報錯。
s1 = {10, 20}
s1.remove(10)
print(s1)
s1.remove(10) # 報錯
print(s1)discard()刪除集合中的指定數(shù)據(jù),如果數(shù)據(jù)不存在也不會報錯。
s1 = {10, 20}
s1.discard(10)
print(s1)
s1.discard(10) # 報錯
print(s1)pop()隨機刪除集合中的某個數(shù)據(jù),并返回這個數(shù)據(jù)。
s1 = {10, 20, 30, 40, 50}
del_num = s1.pop()
print(del_num)
print(s1)4.2.3 查找數(shù)據(jù)
in:判斷數(shù)據(jù)在集合序列not in:判斷數(shù)據(jù)不在集合序列
s1 = {10, 20, 30, 40, 50}
print(10 in s1)
print(10 not in s1)總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python中將字符串轉(zhuǎn)換為列表的常用八種方法
本文介紹了Python中將字符串轉(zhuǎn)換為列表的八種常用方法,包括split()方法、列表解析、正則表達式、str()函數(shù)、map()函數(shù)、re.split()、re.finditer()和逐字符遍歷,感興趣的可以了解一下2024-07-07
基于python實現(xiàn)數(shù)組格式參數(shù)加密計算
這篇文章主要介紹了基于python實現(xiàn)數(shù)組格式參數(shù)加密計算,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
根據(jù)DataFrame某一列的值來選擇具體的某一行方法
今天小編就為大家分享一篇根據(jù)DataFrame某一列的值來選擇具體的某一行方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python使用urllib模塊和pyquery實現(xiàn)阿里巴巴排名查詢
這篇文章主要介紹了python庫urllib及pyquery基本東西的應用,實現(xiàn)阿里巴巴關鍵詞排名的查詢,其中涉及到urllib代理的設置,pyquery對html文檔的解析2014-01-01
python實現(xiàn)生成Word、docx文件的方法分析
這篇文章主要介紹了python實現(xiàn)生成Word、docx文件的方法,結合實例形式分析了Python使用docx模塊操作word文件與docx文件的相關實現(xiàn)技巧,需要的朋友可以參考下2019-08-08

