首页 > 代码库 > Python实战之dict简单练习
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简单练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。