易百教程

32、Python中的字典是什么?

Python字典是一种内置数据类型,它定义了键和值之间的一对一关系。字典包含一对键及其对应的值。 它将元素存储在键值对中。 键是唯一的,而值可以是重复的。 键访问字典元素。

例子:

以下示例包含一些键 country,provincecity。 它们对应的值分别是:中国、海南和海口。

dict = {'country': '中国', 'province': '海南', 'city': '海口'}  
print ("Country: ", dict['country'])    
print ("Hero: ", dict['province'])  
print ("Cartoon: ", dict['city'])

运行结果:

Country:  中国
Province:  海南
City:  海口