基础 | 11个Python字典用法详解
本期给大家带来Python字典11个方法的全面解析,希望对你有所帮助。
字典(Dictionary)是Python提供的一种常用的数据结构,它用于存放具有映射关系的数据,由键(key)和值(value)成对组成,键和值中间以冒号:隔开,项之间用逗号隔开,整个字典由大括号{}括起来,格式如下:dic = {key1 : value1, key2 : value2 }
字典也被称作关联数组或哈希表,下面是几种常见的字典创建方式:
# 方法1dic1 = { 'Author' : 'Python当打之年' , 'age' : 99 , 'sex' : '男' }# 方法2lst = [('Author', 'Python当打之年'), ('age', 99), ('sex', '男')]dic2 = dict(lst)# 方法3dic3 = dict( Author = 'Python当打之年', age = 99, sex = '男')# 方法4list1 = ['Author', 'age', 'sex']list2 = ['Python当打之年', 99, '男']dic4 = dict(zip(list1, list2))
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']
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
1. dict.clear()
clear() 用于清空字典中所有元素(键-值对),对一个字典执行 clear() 方法之后,该字典就会变成一个空字典:
list1 = ['Author', 'age', 'sex']list2 = ['Python当打之年', 99, '男']dic1 = dict(zip(list1, list2))# dic1 = {'Author': 'Python当打之年', 'age': 99, 'sex': '男'}dic1.clear()# dic1 = {}
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': '男'}
import copylist1 = ['Author', 'age', 'sex']list2 = ['Python当打之年', [18,99], '男']dic1 = dict(zip(list1, list2))dic2 = dic1dic3 = 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': '男'}
3. dict.fromkeys()
fromkeys() 使用给定的多个键创建一个新字典,值默认都是 None,也可以传入一个参数作为默认的值:
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() 用于返回指定键的值,也就是根据键来获取值,在键不存在的情况下,返回 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 = Nonephone = dic1.get('phone','12345678')# phone = 12345678
items() 获取字典中的所有键-值对,一般情况下可以将结果转化为列表再进行后续处理:
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># items = [('Author', 'Python当打之年'), ('age', [18, 99]), ('sex', '男')]</class>
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># keys = ['Author', 'age', 'sex']</class>
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]}
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]}
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': '当打之年'}
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># values = ['Python当打之年', [18, 99], '男']</class>