2015-11-21 32 views

回答

4

只需使用enumerate來幫助您的密鑰生成任務和for循環訪問列表中的每個項目。

在這裏你去:

google_dict = dict() 
google_price_data = [317.68,396.05,451.48,428.03,516.26,604.83,520.63,573.48,536.51,542.84,533.85,660.87,728.9] 

for i, item in enumerate(google_price_data, start=1): 
    google_dict[i] = item 

print google_dict 

輸出:

{ 
    1: 317.68, 
    2: 396.05, 
    3: 451.48, 
    4: 428.03, 
    5: 516.26, 
    6: 604.83, 
    7: 520.63, 
    8: 573.48, 
    9: 536.51, 
    10: 542.84, 
    11: 533.85, 
    12: 660.87, 
    13: 728.9 
} 
+0

注意,'i'從0上面的代碼開始,也許'I + 1'作爲重點 – kwarunek

+0

然後他可以使用i + 1 :) –

+1

變異 - 字典理解也派上用場:)'google_dict = {i + 1:item for en,item in enumerate(google_price_data)}' – kwarunek

6
dict(enumerate(google_price_data, start=1)) 
+0

不錯,你有我的投票! –

相關問題