字典是一对映射数据类型,或者理解为关联数组或哈希表,由键-值成对构成,key-value。字典元素用{}括起来。
示例:
>>> dict={'host':'earth'}
>>> dict['port']=80 #增加元素到字典
>>> dict
{'host': 'earth', 'port': 80}
>>> dict['host']
'earth'
>>> dict.keys()
['host', 'port']
>>> dict.values()
['earth', 80]
>>> for key in dict:
print key,dict[key]
host earth
port 80
本文介绍了Python中字典的基本概念及使用方法,包括如何创建字典、向字典中添加元素、获取字典中的键与值等。通过示例演示了字典的常见操作。
31万+

被折叠的 条评论
为什么被折叠?



