码迷,mamicode.com
首页 > 编程语言 > 详细

Python实战之dict简单练习

时间:2017-06-23 18:15:24      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:shang   update   hash   dict   hook   sub   ini   class   删除   

[__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简单练习

标签:shang   update   hash   dict   hook   sub   ini   class   删除   

原文地址:http://www.cnblogs.com/william126/p/7070934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!