2013-03-08 48 views

回答

8

在第一种情况下,你正在做一个list,而其他的你正在做一个dictlist对象是sequencesdict对象是mappings。看看python types页面。

基本上,列出了“地图”的连续整数(从0开始)到一些对象。这样,它们的行为更像是其他语言中的动态数组。事实上,CPython的实现它们作为C.过度分配阵列

dict地图可哈希密钥的一个对象。它们使用哈希表来实现。


还要注意的是,从python2.7开始,你可以使用{}创建组,以及它们是另一个(基本)类型。评论:

[] #empty list 
{} #empty dict 
set() #empty set 

[1] #list with one element 
{'foo':1} #dict with 1 element 
{1} #set with 1 element 

[1, 2] #list with 2 elements 
{'foo':1, 'bar':2} #dict with 2 elements 
{1, 2} #set with 2 elements. 
+0

谢谢。这对我来说很有意义。 – user2054074 2013-03-08 14:26:10

0

关于Python 2.x的

>>> type([]) 
<type 'list'> 
>>> type({}) 
<type 'dict'> 
>>> 

Python的3.x的

>>> type([]) 
<class 'list'> 
>>> type({}) 
<class 'dict'> 
>>> 
相关问题