首页 > 其他 > 详细

【4】字典

时间:2015-05-10 15:33:18      阅读:177      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!