2016-11-15 50 views
0

還有就是我有來源:如何在python中創建自定義字典?

"VirtualizationType": "hvm", 
"Tags": [ 
     { 
      "Value": "Test", 
      "Key": "Name" 
     } 
     ] 

這裏有點我的代碼:與標籤

for item in result: 
    temp = {"Tags": item["Value"], "VirtualizationType": item["VirtualizationType"]} 
     output.append(temp) 

我可以輕鬆導出VirtualizationType但couldn`t做

回答

0

如何在Python中創建字典的示例,從docs

tel = {'jack': 4098, 'sape': 4139} 

在這個例子中jacksnape是鍵,40984139是的值。

值也可以是字符串,並像你的情況字典的名單, 你的代碼可以修改成這樣:

dictionary = {"Virtualization Type" : "hvm", "Tags" : [{"Value" : "Test", "Key" : "Name"}] } #Runs in the python interpreter with no errors 
+0

我有循環行駛的問題。無論如何,我試圖去.get()「標籤」。 –

+0

@ user3729941你應該更多地解釋你的問題。 –

+0

香港專業教育學院找到了答案: '高清find_value(標籤): 值= 「」 如果沒有標籤:返回值 爲標籤在標籤: VAL = tag.get( 「鑰匙」,無) 如果val = =「值」: value = tag [「Value」] 返回值# –