1.通過key獲取value
??dict = {key1: value1, key2:value2}
??dict['key1'] 可獲取到key1對應的value1??
person = {'name': 'tt', 'age': 13}
print(person['age']) # 13
test_dict = {'name': 'll', 'age': 90}
print(test_dict['height']) # 無value時程式報錯,KeyError: 'height'
??通過dict[key] = value ,當key值存在是,會修改原value、當key值不存在時,會將key:value鍵值對添加到字典中;
person = {'name': 'tt', 'age': 13}
person['age'] = 24
print(person) # {'name': 'tt', 'age': 24}
person['height'] = 178
print(person) # {'name': 'tt', 'age': 24, 'height': 178}
2.字典的update()方法
??dict.update(new_dict) 一次添加多個鍵值對,同樣key值存在時更新key對應的value、key值不存在是添加key:value對;
person = {'name': 'tt', 'age': 13}
person.update({'like': 'football', 'age': 134})
print(person) # {'name': 'tt', 'age': 134, 'like': 'football'}
3.字典的setdefault()方法
??dict.setdefault(key, default_value)
??key值存在時,直接回傳對應的value;key值不存在時,將key: default_value鍵值對添加進字典;
??default_value值可省略,默認是None;
person = {'name': 'tt', 'age': 13}
print(person.setdefault('age', 45)) # 13
print(person.setdefault('like', 'football')) # football
print(person) # {'name': 'tt', 'age': 13, 'like': 'football'}
print(person.setdefault('height')) # None
print(person) # {'name': 'tt', 'age': 13, 'like': 'football', 'height': None}
4.獲取字典全部值
??dict.keys() 獲取字典中全部key值,回傳一個偽串列;
??dict.values() 獲取字典中全部value的值,回傳一個偽串列;
??dict.items() 獲取字典所有的鍵值對, 回傳偽串列,各鍵值對存在串列內的元組中;
person = {'name': 'tt', 'age': 13}
print(person.keys()) # dict_keys(['name', 'age'])
print(list(person.keys())) # ['name', 'age'] (可以轉化成串列)
print(person.values()) # dict_values(['tt', 13])
print(list(person.values())) # ['tt', 13]
print(person.items()) # dict_items([('name', 'tt'), ('age', 13)])
print(list(person.items())) # [('name', 'tt'), ('age', 13)]
5.字典的get() 方法
??dict.get(key) 獲取對應value,無key時、默認回傳None;
??dict.get(key, default_value) 無key時,可以指定回傳結果default_value;
person = {'name': 'tt', 'age': 13}
print(person.get('age')) # 13
print(person.get('like')) # None
print(person.get('like', 'no key')) # no key
print(person['like']) # 程式報錯,KeyError: 'like'
# person['like']方式比get()方式可以節省部分時間,不用做key是否存在的判斷(但基本可忽略不計)
# get()方式比person['like']方式,在回傳結果上更友善些
6.字典的洗掉操作
??dict.pop(key) 洗掉key:value對,并回傳value值;key不存在時會報錯;
??dict.clear() 清空字典;
??dict.popitem() 洗掉字典的最后一個鍵值對, 回傳鍵值對存盤在元組中;
??同樣可借助python內置函式del;
person = {'name': 'tt', 'age': 13, 'like': 'football', 'height': 168}
print(person.pop('name')) # tt (操作有回傳值,值是value)
print(person) # {'age': 13, 'like': 'football', 'height': 168}
print(person.popitem()) # ('height', 168)
print(person) # {'age': 13, 'like': 'football'}
del person['like']
print(person) # {'age': 13}
# del person['name'] # KeyError: 'name' (無對應key時,會報錯)
person.clear()
print(person) # {}
7.字典的copy()函式
??復制字典內元素生成新字典;
# dict.copy() 依舊屬于淺拷貝
person = {
'xiaoming': {
'age': 23,
'height': 178
},
'xiaohong': {
'age': 26,
'height': 168
}
}
new_person = person.copy()
new_person['xiaoming']['age'] = 999
print(new_person) # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
print(person) # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
# import copy, copy.deepcopy() 可完成深拷貝操作
import copy
person = {
'xiaoming': {
'age': 23,
'height': 178
},
'xiaohong': {
'age': 26,
'height': 168
}
}
new_person = copy.deepcopy(person)
new_person['xiaoming']['age'] = 999
print(new_person) # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
print(person) # {'xiaoming': {'age': 23, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
8.其它簡單操作
??in ,not in 成員判斷;
??len(dict) 判斷字典長度;
test_dict = {'name': 'll', 'age': 90}
print('name' in test_dict) # True
print(len(test_dict)) # 2
# 字典沒有累加累乘操作
??
總結
????
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518505.html
標籤:其他
上一篇:day01-2-匯入驅動和工具類
下一篇:手動生成dump檔案的方法分享
