Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Dict转换为Dict-Python_Python_Dictionary - Fatal编程技术网

将Dict转换为Dict-Python

将Dict转换为Dict-Python,python,dictionary,Python,Dictionary,我是Python新手,目前正在使用数据字典。 我希望将dict创建为dict,如下所示: dates = {201101:{perf:10, reli:20, qos:300}, 201102:{perf:40, reli:0, qos:30}} 我已经有了这些键,我必须为初始化创建默认值。i、 e: {201101:{perf:0,reli:0},201102:{perf:0,reli:0} 如何启动和更新特定指令,即日期[201101[perf]]=10。 谢谢 首先,需要使用:将键与其值

我是Python新手,目前正在使用数据字典。 我希望将dict创建为dict,如下所示:

dates = {201101:{perf:10, reli:20, qos:300}, 201102:{perf:40, reli:0, qos:30}}
我已经有了这些键,我必须为初始化创建默认值。i、 e:

{201101:{perf:0,reli:0},201102:{perf:0,reli:0}

如何启动和更新特定指令,即日期[201101[perf]]=10。
谢谢

首先,需要使用
将键与其值分开。其次,您的键需要是字符串或数字。例如

dates = { 201101: {'perf': 10, 'reli':20, 'qos': 300} }
首先

dates = {201101{perf=10, reli=20, qos=300}, 201102{perf=40, reli=0, qos=30}}
不是有效的python dict。这是:

dates = {201101: {'perf':10, 'reli':20, 'qos':300}, 201102:{'perf':40, 'reli':0, 'qos':30}}
一旦您启动了dict of dict as:

dates = {201101:{'perf':0, 'reli':0}, 201102:{'perf':0, 'reli':0}}
您可以通过执行以下操作来更新它:

dates[201101]['perf'] = 10
演示:

你的第一句话:

dates={201101:{'perf':0, 'reli':0}, 201102:{'perf':0, 'reli':0}}
更新密钥值的一种方法:

date[201101]={'perf': 10, 'reli':20, 'qos': 300} 

对于运行时值,您需要的另一种方法是setdefault(i,[])或{},而不是[]。这允许您动态创建嵌套字典,而不是“平面”

例如:

    dict = {}
    for i in range(0,10):
        dict.setdefault(i,{})
        # Only now you can use the nested dict of level 1
        for j in range(0,10):
            dict[i][j] = j

这只是一个例子,您可以动态创建任何深度的dict,并使用异类元素作为list或其他dict。

什么是
201101{perf=10,reli=20,qos=300}
@thefourtheye抱歉我的错误
    dict = {}
    for i in range(0,10):
        dict.setdefault(i,{})
        # Only now you can use the nested dict of level 1
        for j in range(0,10):
            dict[i][j] = j