Python?十個字典用法使用技巧歸納
大家好,本期給大家?guī)鞵ython字典11個方法的全面解析,希望對你有所幫助。
字典(Dictionary)是Python提供的一種常用的數(shù)據(jù)結(jié)構(gòu),它用于存放具有映射關(guān)系的數(shù)據(jù),由鍵(key)和值(value)成對組成,鍵和值中間以冒號:隔開,項之間用逗號隔開,整個字典由大括號{}括起來,格式如下:
dic = {key1 : value1, key2 : value2 }
字典也被稱作關(guān)聯(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) 來查看該類包含哪些方法,輸入命令,可以看到如下輸出結(jié)果:
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 的引用,所以輸出結(jié)果是一致的,dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改,注意父子關(guān)系。
拓展深拷貝: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 的引用,所以輸出結(jié)果是一致的;dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改;dic4 進行了深拷貝,遞歸拷貝所有數(shù)據(jù),相當于完全在另外內(nèi)存中新建原字典,所以修改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() 獲取字典中的所有鍵-值對,一般情況下可以將結(jié)果轉(zhuǎn)化為列表再進行后續(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() 返回指定鍵對應(yīng)的值,并在原字典中刪除這個鍵-值對:
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() 類似, 但如果鍵不存在于字典中,將會添加鍵并將值設(shè)為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里,如果被更新的字典中己包含對應(yīng)的鍵-值對,那么原鍵-值對會被覆蓋,如果被更新的字典中不包含對應(yīng)的鍵-值對,則添加該鍵-值對:
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], '男']
到此這篇關(guān)于Python 十個字典用法使用技巧歸納的文章就介紹到這了,更多相關(guān)Python 字典內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python多維/嵌套字典數(shù)據(jù)無限遍歷的實現(xiàn)
下面小編就為大家?guī)硪黄狿ython多維/嵌套字典數(shù)據(jù)無限遍歷的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
pycharm中連接mysql數(shù)據(jù)庫的步驟詳解
在進行Python研發(fā)的時候,pycharm是一個很好的IDE,下面這篇文章主要給大家介紹了pycharm中連接mysql數(shù)據(jù)庫的步驟,文中通過圖文介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-05-05
Pytorch GPU顯存充足卻顯示out of memory的解決方式
今天小編就為大家分享一篇Pytorch GPU顯存充足卻顯示out of memory的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python爬蟲獲取op.gg英雄聯(lián)盟英雄對位勝率的源碼
這篇文章主要介紹了Python爬蟲獲取op.gg英雄聯(lián)盟英雄對位勝率,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
兩行代碼解決Jupyter Notebook中文不能顯示的問題
這篇文章主要介紹了兩行代碼解決Jupyter Notebook中文不能顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

