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'}})])
person = dict({'name':"zhangsan","age":18,"gender":"M"}) print(person) print(dir(person)) # ['__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'] # for k,v in person.items(): # print(k,v) # gender M # age 18 # name zhangsan # # person.clear() # print(person) # {} print(person.copy()) # {'gender': 'M', 'name': 'zhangsan', 'age': 18} print(person.fromkeys(person)) # {'name': None, 'gender': None, 'age': None} print(person.get('age')) # 18 print(person.get('age1')) # None print(person.keys()) # dict_keys(['name', 'gender', 'age']) print(person.values()) # dict_values(['zhangsan', 18, 'M']) print(person.items()) # dict_items([('gender', 'M'), ('name', 'zhangsan'), ('age', 18)]) # print(person.pop('name')) # zhangsan # ,', 'pop', 'popitem', 'setdefault', 'update'] # print(person.popitem()) # {'gender': 'M', 'age': 18} # print(person.setdefault('age')) # 18 # print(person.setdefault('job')) # None # print(person) # {'name': 'zhangsan', 'age': 18, 'job': None, 'gender': 'M'} print(person.update({"age":56,'name':"William"})) # {'age': 56, 'name': 'William', 'gender': 'M'} print(person.__contains__("age")) # True print("*"*50) # print(person.__delitem__('name')) # None # {'age': 56, 'gender': 'M'} print(person.__getitem__('age')) # 56 print(person)
posted on 2017-06-23 17:18 William126 阅读(212) 评论(0) 编辑 收藏 举报