Python 字典中的所有方法及用法

字典是Python中唯一内建的映射类型。字典中没有特殊的顺序,但都是存储在一个特定的键(key)下面,键可以是数字,字符串,甚至是元组
字典的使用
在某些情况下,字典比列表更加适用: 1、表示一个游戏棋盘的状态,每个键都是由坐标值组成的元组 2、存储文件修改时间,用文件名作为键; 3、数字电话\地址薄
创建字典的五种方式
# 字典的创建方式 # 方式一 {key: value} dict1 = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} print(dict1) # 方式二 dict(zip(keys, values)) # dict([(k, v), (k, v)]) dict2 = dict([(‘one‘, 1), (‘two‘, 2), (‘three‘, 3)]) print(dict2) # dict(((k, v), (k, v))) dict3 = dict(((‘one‘, 1), (‘two‘, 2), (‘three‘, 3))) print(dict3) # 方式三 dict(key=value) dict4 = dict(one=1, two=2, three=3) print(dict4) # 方式四 字典推导式 lst1 = [‘one‘, ‘two‘, ‘three‘] lst2 = [1, 2, 3] dict5 = {lst1[i]: lst2[i] for i in range(len(lst1))} print(dict5) # 方式五 fromkeys # 没有指定value,默认None dict6 = dict.fromkeys([‘k1‘, ‘k2‘, ‘k3‘]) print(dict6) # {‘k1‘: None, ‘k2‘: None, ‘k3‘: None} # 指定value dict7 = dict.fromkeys([‘k1‘, ‘k2‘, ‘k3‘], ‘value‘) print(dict7) # {‘k1‘: ‘value‘, ‘k2‘: ‘value‘, ‘k3‘: ‘value‘}
字典的示例
1. setdefault使用
# 有则不变,无则增加 dic1 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic1.setdefault(‘age‘, 45) # {‘name‘: ‘lucy‘, ‘age‘: 18} dic1.setdefault(‘sex‘, ‘男‘) # {‘name‘: ‘lucy‘, ‘age‘: 18, ‘sex‘: ‘男‘}
2. pop使用
# 按照键删除键值对,有返回值 # 如果设置了第二个参数则无论字典中有无此键都不会报错 dic2 = {‘name‘: ‘lucy‘, ‘age‘: 18} ret2 = dic2.pop(‘age‘) print(ret2) # 18 print(dic2) # {‘name‘: ‘lucy‘} dic3 = {‘name‘: ‘lucy‘, ‘age‘: 18} ret3 = dic3.pop(‘sex‘, ‘没有此键‘) print(ret3) # 没有此键 print(dic3) # {‘name‘: ‘lucy‘, ‘age‘: 18}
3. clear使用
# 清空 dic4 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic4.clear() print(dic4) # {}
4. get使用
dic5 = {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby_list‘: [‘study‘, ‘play‘, ‘shopping‘]} l1 = dic5.get(‘hobby_list‘) print(l1) # [‘study‘, ‘play‘, ‘shopping‘] l2 = dic5.get(‘sex‘) print(l2) # None l3 = dic5.get(‘sex‘, ‘没有此键‘) print(l3) # 没有此键
5. keys()使用
dic6 = {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby_list‘: [‘study‘, ‘play‘, ‘shopping‘]} # 1. 特殊的数据类型,类似于列表,但是没有索引 print(dic6.keys(), type(dic6.keys())) # dict_keys([‘name‘, ‘age‘, ‘hobby_list‘]) <class ‘dict_keys‘> # 2. 可以转化成列表 l1 = list(dic6.keys()) print(l1) # [‘name‘, ‘age‘, ‘hobby_list‘] # 3. 循环遍历 for k in dic6.keys(): print(k, end=‘,‘) # name,age,hobby_list, for k in dic6: print(k, end=‘,‘) # name,age,hobby_list,
6. values()使用
dic7 = {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby_list‘: [‘study‘, ‘play‘, ‘shopping‘]} # 1. 特殊的数据类型,类似于列表,但是没有索引 print(dic7.values()) # dict_values([‘lucy‘, 18, [‘study‘, ‘play‘, ‘shopping‘]]) # 2. 可以转化成列表 print(list(dic7.values())) # [‘lucy‘, 18, [‘study‘, ‘play‘, ‘shopping‘] # 3. 循环遍历 for val in dic7.values(): print(val, end=‘,‘) # lucy,18,[‘study‘, ‘play‘, ‘shopping‘],
7. items()使用
dic8 = {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby_list‘: [‘study‘, ‘play‘, ‘shopping‘]} print(dic8.items()) # dict_items([(‘name‘, ‘lucy‘), (‘age‘, 18), (‘hobby_list‘, [‘study‘, ‘play‘, ‘shopping‘])]) for it in dic8.items(): print(it) # (‘name‘, ‘lucy‘) # (‘age‘, 18) # (‘hobby_list‘, [‘study‘, ‘play‘, ‘shopping‘]) # 利用了元组的拆包 for key, value in dic8.items(): print(key, value) # name lucy # age 18 # hobby_list [‘study‘, ‘play‘, ‘shopping‘]
8. update()使用
# 更新,有则覆盖,无则添加 # 1. 增加键值对 # 方式一: dic9 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic9.update(hobby=‘运动‘, hight=178) print(dic9) # {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby‘: ‘运动‘, ‘hight‘: 178} # 方式二: dic10 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic10.update([(‘hobby‘, ‘运动‘), (‘hight‘, 178)]) print(dic10) # {‘name‘: ‘lucy‘, ‘age‘: 18, ‘hobby‘: ‘运动‘, ‘hight‘: 178} # 方式三: dic11 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic12 = {‘name‘: ‘tom‘, ‘hight‘: 178} dic11.update(dic12) print(dic11) # {‘name‘: ‘tom‘, ‘age‘: 18, ‘hight‘: 178} # 2. 修改键所对应的值 dic13 = {‘name‘: ‘lucy‘, ‘age‘: 18} dic13.update(name=‘tom‘) print(dic13) # {‘name‘: ‘tom‘, ‘age‘: 18}
9. fromkeys()使用
# 没有指定value,默认None dict14 = dict.fromkeys([‘k1‘, ‘k2‘, ‘k3‘]) print(dict14) # {‘k1‘: None, ‘k2‘: None, ‘k3‘: None} # 指定value dict14 = dict.fromkeys([‘k1‘, ‘k2‘, ‘k3‘], ‘value‘) print(dict14) # {‘k1‘: ‘value‘, ‘k2‘: ‘value‘, ‘k3‘: ‘value‘}
10. copy使用
# 浅拷贝 dict15 = dict(one=1, two=2, three=3) dict16 = dict15.copy() print(dict16)
11. popitem()使用
# 随机删除字典中的一对 键和值 dict17 = dict(one=1, two=2, three=3) a = dict17.popitem() print(a) print(dict17)
到此这篇关于Python 字典中的所有方法及用法的文章就介绍到这了,更多相关Python 字典方法及用法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关推荐