Python实战之dict简单练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实战之dict简单练习相关的知识,希望对你有一定的参考价值。
[‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘] my_dict = { ‘user1‘:"zhangsan", ‘user2‘:"lisi", ‘user3‘:"wangermazi", } print(my_dict) # {‘user2‘: ‘lisi‘, ‘user3‘: ‘wangermazi‘, ‘user1‘: ‘zhangsan‘},无序输出 my_dict[‘user4‘] = "xiaotaoqi" print(my_dict) # {‘user3‘: ‘wangermazi‘, ‘user4‘: ‘xiaotaoqi‘, ‘user2‘: ‘lisi‘, ‘user1‘: ‘zhangsan‘},增加 my_dict[‘user1‘] = "ZHANGSAN" print(my_dict) # {‘user3‘: ‘wangermazi‘, ‘user2‘: ‘lisi‘, ‘user4‘: ‘xiaotaoqi‘, ‘user1‘: ‘ZHANGSAN‘},修改 # 删除三法pop,del,popitem my_dict.pop("user1") print(my_dict) # {‘user2‘: ‘lisi‘, ‘user3‘: ‘wangermazi‘, ‘user4‘: ‘xiaotaoqi‘} del my_dict[‘user2‘] print(my_dict) # {‘user3‘: ‘wangermazi‘, ‘user4‘: ‘xiaotaoqi‘} my_dict.popitem() print(my_dict) # {‘user2‘: ‘lisi‘, ‘user3‘: ‘wangermazi‘, ‘user1‘: ‘ZHANGSAN‘} city = { ‘1‘:{ ‘beijing‘:{ ‘chaoyang‘:"xizhimen", ‘haidian‘:‘xibeiwang‘, ‘tongzhou‘:‘shuxi‘ } }, ‘2‘:"shanghai", ‘3‘:"liaoning" } print(city[‘1‘][‘beijing‘][‘haidian‘]) # xibeiwang print(city.keys()) # dict_keys([‘1‘, ‘2‘, ‘3‘]) print(city.values()) # dict_values([{‘beijing‘: {‘haidian‘: ‘xibeiwang‘, ‘tongzhou‘: ‘shuxi‘, ‘chaoyang‘: ‘xizhimen‘}}, ‘shanghai‘, ‘liaoning‘]) print(city.items()) # dict_items([(‘3‘, ‘liaoning‘), (‘2‘, ‘shanghai‘), (‘1‘, {‘beijing‘: {‘tongzhou‘: ‘shuxi‘, ‘chaoyang‘: ‘xizhimen‘, ‘haidian‘: ‘xibeiwang‘}})])
以上是关于Python实战之dict简单练习的主要内容,如果未能解决你的问题,请参考以下文章