python3.0 字典key排序
程序员文章站
2022-04-28 10:38:22
idle 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3,...
idle 3.0
>>> dic = {"aa":1,"bb":2,"ab":3}
>>> dic
{'aa': 1, 'ab': 3, 'bb': 2}
>>> for k in sorted(dic.keys()):
print (k)
aa
ab
-----------------------------------------------
字典对象其实就是键-值对
下面是字典对象的添加,修改,删除
(修改与添加方法相同,当key值不存在的时候添加)
>>> dic["cc"] = 4
>>> dic
{'aa': 1, 'cc': 4, 'ab': 3, 'bb': 2}
>>> dic["bc"] = 5
>>> dic
{'aa': 1, 'cc': 4, 'ab': 3, 'bb': 2, 'bc': 5}
>>> dic["cc"] = 6
>>> dic
{'aa': 1, 'cc': 6, 'ab': 3, 'bb': 2, 'bc': 5}
>>> del dic["cc"]
>>> dic
{'aa': 1, 'ab': 3, 'bb': 2, 'bc': 5}
>>> dic = {"aa":1,"bb":2,"ab":3}
>>> dic
{'aa': 1, 'ab': 3, 'bb': 2}
>>> for k in sorted(dic.keys()):
print (k)
aa
ab
-----------------------------------------------
字典对象其实就是键-值对
下面是字典对象的添加,修改,删除
(修改与添加方法相同,当key值不存在的时候添加)
>>> dic["cc"] = 4
>>> dic
{'aa': 1, 'cc': 4, 'ab': 3, 'bb': 2}
>>> dic["bc"] = 5
>>> dic
{'aa': 1, 'cc': 4, 'ab': 3, 'bb': 2, 'bc': 5}
>>> dic["cc"] = 6
>>> dic
{'aa': 1, 'cc': 6, 'ab': 3, 'bb': 2, 'bc': 5}
>>> del dic["cc"]
>>> dic
{'aa': 1, 'ab': 3, 'bb': 2, 'bc': 5}
上一篇: 网站建好了都要向哪些搜索引擎提交?
下一篇: Python 调用DLL操作抄表机