2013-03-08 49 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'> 
>>> 
相關問題