Python
作者:互联网
Python的第九天
一、数据类型-dict
1、增加
>>> dictionary["names"] = "alex"
>>> dictionary["age"] = 22
2、删除
>>> dictionary.pop("age")
>>> del dictionary["names"]
>>> dictionary.clear()
3、修改
>>> dictionary["names"] = "wang"
>>> dictionary
{'names': 'wang'}
>>> dictionary["names"] = "liang"
>>> dictionary
{'names': 'liang'}
4、查操作
查value d["alex"]
查key "alex" in d
返回所有的key d.keys()
返回所有的value d.values()
返回所有的元素 d.items()
5、循环
打印key和value
>>> for k in d:
... print(k,d[k])
...
alex [22, 2000]
liang [20, 1000]
>>> for k in d.keys():
... print(k)
>>> for k in d:
... print(k)
6、求长度----------能求列表、字符串、字典的长度,不能求数字的长度
>>> len(d)
2
>>> len("alex")
4
>>> len([22,2000])
2
7、嵌套
>>> d["jack"] = {"salary" : 5000}
>>> d
{'alex': [22, 2000], 'liang': [20, 1000], 'jack': {'salary': 5000}}
>>> d["jack"]
{'salary': 5000}
>>> d["jack"]["salary"]
5000
标签:5000,alex,22,dictionary,Python,names,liang 来源: https://www.cnblogs.com/sxy2021/p/14332279.html