标签:
字典和列表元组不同,列表用[],元组用();字典用{};
字典用大括号。
不过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
>>>
标签:
原文地址:http://www.cnblogs.com/legexuexi/p/4492234.html