Python?十個字典用法使用技巧歸納
大家好,本期給大家?guī)鞵ython字典11個方法的全面解析,希望對你有所幫助。
字典(Dictionary)是Python提供的一種常用的數(shù)據(jù)結構,它用于存放具有映射關系的數(shù)據(jù),由鍵(key)和值(value)成對組成,鍵和值中間以冒號:隔開,項之間用逗號隔開,整個字典由大括號{}括起來,格式如下:
dic = {key1 : value1, key2 : value2 }
字典也被稱作關聯(lián)數(shù)組或哈希表,下面是幾種常見的字典創(chuàng)建方式:
# 方法1
dic1 = { 'Author' : 'Python' , 'age' : 99 , 'sex' : '男' }
# 方法2
lst = [('Author', 'Python'), ('age', 99), ('sex', '男')]
dic2 = dict(lst)
# 方法3
dic3 = dict( Author = 'Python', age = 99, sex = '男')
# 方法4
list1 = ['Author', 'age', 'sex']
list2 = ['Python', 99, '男']
dic4 = dict(zip(list1, list2))
字典創(chuàng)建的方式還有很多種,這里不再贅述。
字典由 dict 類代表,可以使用 dir(dict) 來查看該類包含哪些方法,輸入命令,可以看到如下輸出結果:
print('methods = ',methods)
methods = ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
字典的方法和屬性有很多種,這里我們重點介紹以下11種方法:
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
1.dict.clear()
clear() 用于清空字典中所有元素(鍵-值對),對一個字典執(zhí)行 clear() 方法之后,該字典就會變成一個空字典:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', 99, '男']
dic1 = dict(zip(list1, list2))
# dic1 = {'Author': 'Python', 'age': 99, 'sex': '男'}
dic1.clear()
# dic1 = {}
2.dict.copy()
copy() 用于返回一個字典的淺拷貝:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', 99, '男']
dic1 = dict(zip(list1, list2))
dic2 = dic1 # 淺拷貝: 引用對象
dic3 = dic1.copy() # 淺拷貝:深拷貝父對象(一級目錄),子對象(二級目錄)不拷貝,還是引用
dic1['age'] = 18
# dic1 = {'Author': 'Python', 'age': 18, 'sex': '男'}
# dic2 = {'Author': 'Python', 'age': 18, 'sex': '男'}
# dic3 = {'Author': 'Python', 'age': 99, 'sex': '男'}
其中 dic2 是 dic1 的引用,所以輸出結果是一致的,dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改,注意父子關系。
拓展深拷貝:copy.deepcopy()
import copy
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
dic2 = dic1
dic3 = dic1.copy()
dic4 = copy.deepcopy(dic1)
dic1['age'].remove(18)
dic1['age'] = 20
# dic1 = {'Author': 'Python', 'age': 20, 'sex': '男'}
# dic2 = {'Author': 'Python', 'age': 20, 'sex': '男'}
# dic3 = {'Author': 'Python', 'age': [99], 'sex': '男'}
# dic4 = {'Author': 'Python', 'age': [18, 99], 'sex': '男'}
dic2 是 dic1 的引用,所以輸出結果是一致的;dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改;dic4 進行了深拷貝,遞歸拷貝所有數(shù)據(jù),相當于完全在另外內存中新建原字典,所以修改dic1不會影響dic4的數(shù)據(jù)
3.dict.fromkeys()
fromkeys() 使用給定的多個鍵創(chuàng)建一個新字典,值默認都是 None,也可以傳入一個參數(shù)作為默認的值:
list1 = ['Author', 'age', 'sex']
dic1 = dict.fromkeys(list1)
dic2 = dict.fromkeys(list1, 'Python')
# dic1 = {'Author': None, 'age': None, 'sex': None}
# dic2 = {'Author': 'Python', 'age': 'Python', 'sex': 'Python'}
4.dict.get()
get() 用于返回指定鍵的值,也就是根據(jù)鍵來獲取值,在鍵不存在的情況下,返回 None,也可以指定返回值:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
Author = dic1.get('Author')
# Author = Python
phone = dic1.get('phone')
# phone = None
phone = dic1.get('phone','12345678')
# phone = 12345678
5.dict.items()
items() 獲取字典中的所有鍵-值對,一般情況下可以將結果轉化為列表再進行后續(xù)處理:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
items = dic1.items()
print('items = ', items)
print(type(items))
print('items = ', list(items))
# items = dict_items([('Author', 'Python'), ('age', [18, 99]), ('sex', '男')])
# <class 'dict_items'>
# items = [('Author', 'Python'), ('age', [18, 99]), ('sex', '男')]
6.dict.keys()
keys() 返回一個字典所有的鍵:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
keys = dic1.keys()
print('keys = ', keys)
print(type(keys))
print('keys = ', list(keys))
# keys = dict_keys(['Author', 'age', 'sex'])
# <class 'dict_keys'>
# keys = ['Author', 'age', 'sex']
7.dict.pop()
pop() 返回指定鍵對應的值,并在原字典中刪除這個鍵-值對:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
sex = dic1.pop('sex')
print('sex = ', sex)
print('dic1 = ',dic1)
# sex = 男
# dic1 = {'Author': 'Python', 'age': [18, 99]}
8.dict.popitem()
popitem() 刪除字典中的最后一對鍵和值:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
dic1.popitem()
print('dic1 = ',dic1)
# dic1 = {'Author': 'Python', 'age': [18, 99]}
9.dict.setdefault()
setdefault() 和 get() 類似, 但如果鍵不存在于字典中,將會添加鍵并將值設為default:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
dic1.setdefault('Author', '')
print('dic1 = ',dic1)
# dic1 = {'Author': 'Python', 'age': [18, 99], 'sex': '男'}
dic1.setdefault('name', '')
print('dic1 = ',dic1)
# dic1 = {'Author': 'Python', 'age': [18, 99], 'sex': '男', 'name': ''}
10.dict.update(dict1)
update() 字典更新,將字典dict1的鍵-值對更新到dict里,如果被更新的字典中己包含對應的鍵-值對,那么原鍵-值對會被覆蓋,如果被更新的字典中不包含對應的鍵-值對,則添加該鍵-值對:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
print('dic1 = ',dic1)
# dic1 = {'Author': 'Python', 'age': [18, 99], 'sex': '男'}
list3 = ['Author', 'phone' ]
list4 = ['', 12345678]
dic2 = dict(zip(list3, list4))
print('dic2 = ',dic2)
# dic2 = {'Author': '', 'phone': 12345678}
dic1.update(dic2)
print('dic1 = ',dic1)
# dic1 = {'Author': '', 'age': [18, 99], 'sex': '男', 'phone': 12345678}
11.dict.values()
values() 返回一個字典所有的值:
list1 = ['Author', 'age', 'sex']
list2 = ['Python', [18,99], '男']
dic1 = dict(zip(list1, list2))
values = dic1.values()
print('values = ', values)
print(type(values))
print('values = ', list(values))
# values = dict_values(['Python', [18, 99], '男'])
# <class 'dict_values'>
# values = ['Python', [18, 99], '男']
到此這篇關于Python 十個字典用法使用技巧歸納的文章就介紹到這了,更多相關Python 字典內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符
這篇文章主要為大家詳細介紹了python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
python flask幾分鐘實現(xiàn)web服務的例子
今天小編就為大家分享一篇python flask幾分鐘實現(xiàn)web服務的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python向json中追加數(shù)據(jù)的兩種方法總結
JSON用來存儲和交換文本信息,比xml更小/更快/更易解析,下面這篇文章主要給大家介紹了關于python向json中追加數(shù)據(jù)的兩種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05
Python的Django框架中的數(shù)據(jù)庫配置指南
這篇文章主要介紹了Python的Django框架中的數(shù)據(jù)庫配置指南,文中舉了Python內置的SQLite的示例,需要的朋友可以參考下2015-07-07
pip matplotlib報錯equired packages can not be built解決
這篇文章主要介紹了pip matplotlib報錯equired packages can not be built解決,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
?python中pandas讀取csv文件?時如何省去csv.reader()操作指定列步驟
這篇文章主要介紹了?python中pandas讀取csv文件?時如何省去csv.reader()操作指定列步驟,對正在工作的你可能有一定的幫助,需要的朋友可以參考一下2022-01-01

