2014-02-17 266 views
-1

我有一個字符串,我想變成字典。將字符串轉換爲字典python

我的字符串:

minmax = "min: " + min(allvalueslist) + ", " + "max: " + max(allvalueslist) 

輸出:

min: 29.47, max: 35.96 

我想到的是,下面的代碼會工作,但它失敗:

minmax = dict("min: " + min(allvalueslist) + ", " + "max: " + max(allvalueslist)) 

錯誤:

ValueError: dictionary update sequence element #0 has bad length; 2 is required 
+6

Errr:'極大極小= { '分鐘':分鐘(your_list) ,'max':max(your_list)}'? –

+5

試試'minmax = dict(min = min(allvalueslist),max = max(allvalueslist))' –

+0

試圖通過字符串傳遞的任何原因?爲什麼不做minmax = dict(min = min(allvalueslist),max = max(allvalueslist))? –

回答

1

嘗試這種情況:

minmax = { 
    "min":min(allvalueslist), 
    "max":max(allvalueslist) 
} 

而且爲了使用它:

a = minmax["min"] 
b = minmax["max"] 
0

與嘗試:

minmax = {"min": min(allvalueslist), "max": max(allvalueslist)}