2017-04-24 47 views

回答

0

所得類型由周圍括號定義:

>>> dic = ((I, (I**3)) for I in range(25)) 
>>> type(dic) 
<class 'generator'> 

>>> dic = [(I, (I**3)) for I in range(25)] 
>>> type(dic) 
<class 'list'> 

>>> dic = {I: (I**3) for I in range(25)} 
>>> type(dic) 
<class 'dict'> 
+0

謝謝你,這是簡單和夢幻般的解釋maurice,:) thumbsup,它清除了我的疑問 – Nagesh

1

如果你要產生大量的數據,但如果你的內存需要一個所有你只需要一次一個,那麼你可以使用發電機,因爲你在你的代碼

dic= ((I, (I**3)) for I in range(25)) 
# type of dic is a generator 

但數據以列表形式,然後使用括號代替使用假設。

dic= [(I, (I**3)) for I in range(25)] 
# type of dic is list 

結果:[(0,0),(1,1),(2,8),(3,27),(4,64),(5,125),(6 (7,243),(8,512),(9,729),(10,1000),(11,1331),(12,1728),(13,2197),(14,2744), ),(15,3335),(16,4096),(17,4913),(18,5832),(19,6859),(20,8000),(21,9261),(22,10648), (23,12167),(24,13824)]

+0

我明白了,謝謝! – Nagesh

相關問題