将字符串转换为dict python

时间:2014-02-17 13:37:23

标签: python string dictionary

我有一个字符串,我想变成一个字典。

我的字符串:

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

为了使用它:

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

答案 1 :(得分:0)

尝试:

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