Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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
如何使用python更新字典中dictionary的值_Python_Dictionary - Fatal编程技术网

如何使用python更新字典中dictionary的值

如何使用python更新字典中dictionary的值,python,dictionary,Python,Dictionary,我有: 现在更新后,我希望它是: new_dict['a1']['Road_Type']=[0,0,0,0,0,0,0] new_dict['a2']['Road_Type']=[0,0,0,0,0,0,0] new_dict['a3']['Road_Type']=[0,0,0,0,0,0,0] new_dict['a4']['Road_Type']=[0,0,0,0,0,0,0] 我的代码: new_dict['a1']['Road_Type']=[0,0,0,0,0,0,0] new_di

我有:

现在更新后,我希望它是:

new_dict['a1']['Road_Type']=[0,0,0,0,0,0,0]
new_dict['a2']['Road_Type']=[0,0,0,0,0,0,0]
new_dict['a3']['Road_Type']=[0,0,0,0,0,0,0]
new_dict['a4']['Road_Type']=[0,0,0,0,0,0,0]
我的代码:

new_dict['a1']['Road_Type']=[0,0,0,0,0,0,0]
new_dict['a2']['Road_Type']=[5,0,0,0,0,0,0]
new_dict['a3']['Road_Type']=[0,0,0,0,0,0,0]
new_dict['a4']['Road_Type']=[0,0,0,0,0,0,0]
但结果是:

kk=new_dict['a2']['Road_Type']
kk[0]=5
new_dict['a2']['Road_Type']=kk

所有值都在更新,因此如何更新特定值。

只需分别更新
a2

new_dict['a1']['Road_Type']=[5,0,0,0,0,0,0]
new_dict['a2']['Road_Type']=[5,0,0,0,0,0,0]
new_dict['a3']['Road_Type']=[5,0,0,0,0,0,0]
new_dict['a4']['Road_Type']=[5,0,0,0,0,0,0]
输出:

new_dict = {
    'a1': {'Road_Type': [0,0,0,0,0,0,0]},
    'a2': {'Road_Type': [0,0,0,0,0,0,0]},
    'a3': {'Road_Type': [0,0,0,0,0,0,0]},
    'a4': {'Road_Type': [0,0,0,0,0,0,0]},
}

new_dict['a2']['Road_Type'][0] = 5

print(new_dict)

根据您对问题的评论,您犯了一个错误,因为您不知道Python是如何工作的。在这个例子中,我将使它更简单,但这也代表了当您有
新的dict[a][b]…[n]
时的情况

以下是您可能是如何生成词典的:

{'a1': {'Road_Type': [0, 0, 0, 0, 0, 0, 0]}, 
 'a2': {'Road_Type': [5, 0, 0, 0, 0, 0, 0]}, 
 'a3': {'Road_Type': [0, 0, 0, 0, 0, 0, 0]}, 
 'a4': {'Road_Type': [0, 0, 0, 0, 0, 0, 0]}}
然而,这会将每个
new_dict[p]
绑定到相同的
lst
,即每个
new_dict[p]
值引用相同的
列表实例

您必须为每个
新目录[p]
生成新列表

以下是您应该如何生成它:

lst = [0, 0, 0, 0, 0, 0, 0]
new_dict = []
for p in range(N):
    new_dict[p] = lst
填充词典后,您可以用一行编辑它:

new_dict = {}

for p in range(N):
    new_dict[p] = [0, 0, 0, 0, 0, 0, 0]
请尝试以下代码: 代码中有一个小错误,您更新了'a2'的索引0,然后将指向'a2'的新目录的'kk'赋值

节目:

new_dict['a1']['RoadType'][0] = 5
new_dict = {}
new_dict['a1']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a2']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a3']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a4']= {'Road_Type': [0,0,0,0,0,0,0]}


kk=new_dict['a2']['Road_Type']
kk[0]=5

print(new_dict)
输出:

new_dict['a1']['RoadType'][0] = 5
new_dict = {}
new_dict['a1']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a2']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a3']= {'Road_Type': [0,0,0,0,0,0,0]}
new_dict['a4']= {'Road_Type': [0,0,0,0,0,0,0]}


kk=new_dict['a2']['Road_Type']
kk[0]=5

print(new_dict)

请显示创建内部列表并分配给词典的代码。因为看起来您多次存储对同一列表的引用。我只想在['a2']中更改它@OmarEinea@dhke内部列表类似于list=[0,0,0,0,0,0,0],然后通过循环我将它们分配为new_dict[p]['Road_Type]=listatt=['Road_Type','Speed_limit','Junction_Detail','Junction_Control']dd=pd.read_csv('finalstring.csv')r=dd['Police_Force']my_set=set()ll=[0,0,0,0,0,0,0]new_dict={}在r:my_set.add(i)new_dict[i]={}在att:new_dict[i][j]=ll new u dict['a5'['Road_Type']0]=5个打印(new dict)仍然显示相同的结果={}对于附件中的j:new_dict[i][j]=[0,0,0,0,0,0,0]
。很难说这里发生了什么,因为我不知道finalstring.csv是什么样子。非常感谢你的评论。我能知道为什么我的代码不起作用吗?@SudhanshuBhandarwar你把每个字典键都设置为同一个对象
l1
。所以每个键都引用同一个对象。无论何时更新,它们都会更新。如果在每次迭代中将每个键设置为
[0,0,0,0,0,0,0,0]
,则它们都引用不同的对象。