码迷,mamicode.com
首页 > 其他好文 > 详细

【4】字典

时间:2015-05-10 15:36:30      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

字典和列表元组不同,列表用[],元组用();字典用{};

字典用大括号。

不过python核心编程中说到过,字典是python中的映射数据类型,工作原理类似perl中的关系数组或哈希表,由键-值对构成(key-value);

几乎所有类型的python对象都可以用作键,不过一般以数字或者字符串最为常用。

值可以是任意类型的python对象,字典元素用大括号{}包裹。

 

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> aDict={‘host‘:‘ealth‘}
>>> aDict[‘port‘]=80
>>> aDict
{‘host‘: ‘ealth‘, ‘port‘: 80}
>>> aDict.keys()
[‘host‘, ‘port‘]
>>> aDict[‘host‘]
‘ealth‘
>>> for key in aDict:
... print key,aDict[key]
File "<stdin>", line 2
print key,aDict[key]
^
IndentationError: expected an indented block
>>> for key in aDict:
... print key,aDict[key]
...
host ealth
port 80
>>>

【4】字典

标签:

原文地址:http://www.cnblogs.com/legexuexi/p/4492234.html

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