当前位置: 移动技术网 > IT编程>脚本编程>Python > 小白学python3----字典dict

小白学python3----字典dict

2020年07月03日  | 移动技术网IT编程  | 我要评论

小白学python3----字典dict

本人小白自学python的历程
主要都是通过网站(菜鸟教程)和b站视频学习的

这里我简单介绍一下python字典(dict)
字典是python中一种可变容器模型,字典的每个键值key对应的一个value值,键必须是唯一的,而值不一定。

创建空字典

#1.创建一个空字典(有两种方法)
dct1=dict()
dct2={}
print(type(dct1))
print(type(dct2))

#result:<class 'dict'>
#result:<class 'dict'>

这边简单列举一下字典函数&方法
python字典函数:
len(dict) 计算字典里元素个数,即键的总数
str(dict) 以字符串的格式输出字典
python字典方法:
dict.clear() 删除字典里的所有元素
dict.copy() 浅拷贝字典
dict.fromkeys() 创建一个新字典,以序列作为字典的键,值默认是None
dict.get(key,default=None) 返回字典中指定键对应的值,若不存在,则返回default值
key in dict 判断某个键是否在字典中,存在则返回True,否则为False
dict.items() 以列表返回可遍历的(键,值)的元组
dict.keys() 返回一个迭代器,可以使用list()来转换成列表,获取key值的列表
dict.values 返回一个迭代器,可以使用list()来转换成列表,获取value值的列表
dict.setdefault(key,default=None) 和dict.get()类似,但若键不存在,则会添加键并赋予default值
dict.update(dict2) 把字典二的键/值更新到字典一(合并字典)
dict.pop(key,defalut) 删除字典中指定的key对应的值,并返回被删除key所对应的value值,指定的key不存在,则返回default值
dict.popitem() 随机删除一组键与值,并返回被删除的键与值


下面是字典函数&方法的简单使用介绍

python字典函数

1:len(dict)
功能:计算字典里元素个数,即键的总数
dct={'Name':'我','Age':19,'Gender':'male'}
print(len(dct))

#result:3
2:str(dict)
功能:以字符串的格式输出字典
dct={'Name':'我','Age':19,'Gender':'male'}
print(str(dct))
print(type(str(dct)))

#result:{'Name': '我', 'Age': 19, 'Gender': 'male'}
#result:<class 'str'>

python字典方法

1:dict.clear()
功能:删除字典里的所有元素
dct={'Name':'我','Age':19,'Gender':'male'}
dct.clear()
print(dct)

#result:{}
2:dict.copy()
功能:浅拷贝字典
dct1={'Name':'我','Age':19,'Gender':'male'}
dct2=dct1.copy()
print(dct2)

#result:{'Name': '我', 'Age': 19, 'Gender': 'male'}
3:dict.fromkeys()
功能:创建一个新字典,以序列作为字典的键,值默认是None
#3.dict.fromkeys()       创建一个新字典,以序列作为字典的键,值默认是None
seq=('Name','Age','Gender')    #先创建一个元组
dct=dict.fromkeys(seq,'不详')
print(dct)

#result:{'Name': '不详', 'Age': '不详', 'Gender': '不详'}
4:dict.get(key,default=None)
功能:返回字典中指定键对应的值,若不存在,则返回default值
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.get('Name'))
print(dct.get('Class'))

#result:我
#result:None
5:key in dict
功能:判断某个键是否在字典中,存在则返回True,否则为False
dct={'Name':'我','Age':19,'Gender':'male'}
print('Age' in dct)
print('Class' in dct)

#result:True
#result:False
6:dict.items()
功能:以列表返回可遍历的(键,值)的元组
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.items())

#reuult:dict_items([('Name', '我'), ('Age', 19), ('Gender', 'male')])
7:dict.keys()
功能:返回一个迭代器,可以使用list()来转换成列表,获取key值的列表
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.keys())
print(list(dct.keys()))

#result:dict_keys(['Name', 'Age', 'Gender'])
#result:['Name', 'Age', 'Gender'] 
8:dict.values
功能:返回一个迭代器,可以使用list()来转换成列表,获取value值的列表
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.values())
print(list(dct.values()))

#result:dict_values(['我', 19, 'male'])
#result:['我', 19, 'male']
9:dict.setdefault(key,default=None)
功能:和dict.get()类似,但若键不存在,则会添加键并赋予default值
dct={'Name':'我','Age':19,'Gender':'male'}
dct.setdefault('Class',4)
print(dct)

#result:{'Name': '我', 'Age': 19, 'Gender': 'male', 'Class': 4}
10:dict.update(dict2)
功能:把字典二的键/值更新到字典一(合并字典)
dct1={'Name':'我','Age':18,'Gender':'male'}
dct2={'Gender':'male','Age':19}
dct1.update(dct2)
print(dct1)

#result:{'Name': '我', 'Age': 19, 'Gender': 'male'}
11:dict.pop(key,defalut)
功能:删除字典中指定的key对应的值,并返回被删除key所对应的value值,指定的key不存在,则返回defalut值
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.pop('Name'))   #若删除的key不存在,则一定要添加default,否则会报错
print(dct)

#result:我
#result:{'Age': 19, 'Gender': 'male'}
12:dict.popitem()
功能:随机删除一组键与值,并返回被删除的键与值
dct={'Name':'我','Age':19,'Gender':'male'}
print(dct.popitem())     #被删除的一组
print(dct)

#result:('Gender', 'male')
#result:{'Name': '我', 'Age': 19}

这就是我学习python3的历程中对字典的学习,在此记录。
这里附上列表的传送门:https://blog.csdn.net/weixin_46791942/article/details/107064905

更加详细的信息可以在菜鸟教程上学习
附上网站:https://www.runoob.com/python3/python3-dictionary.html

本文地址:https://blog.csdn.net/weixin_46791942/article/details/107085495

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网