字典

https://www.runoob.com/python/python-dictionary.html

字典是一种可变容器模型。
字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
 
 
输出时顺序有变,不清楚为什么!
示例1:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
dict[(12,13)] =555
dict.setdefault(98,'9988')
print dict[98]
print dict
 
输出:
9988
{(12, 13): 555, 98: '9988', 'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}
 
示例2:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
for i in dict.keys():
    i=i+'1'
    print i
输出:
Beth1
Alice1
Cecil1
 
 
posted @ 2019-10-29 08:55  数之美  阅读(146)  评论(0编辑  收藏  举报