[英]how to update the value of dictionary in a dictionary using python
I have:我有:
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]
Now after updating I want it to be:现在更新后我希望它是:
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]
My code:我的代码:
kk=new_dict['a2']['Road_Type']
kk[0]=5
new_dict['a2']['Road_Type']=kk
but the result is:但结果是:
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]
all value are getting updated, so how can I update particular value.所有值都在更新,那么我如何更新特定值。
Based on your comments on your question, you are making a mistake due to not knowing how Python works.根据您对问题的评论,您由于不知道 Python 的工作原理而犯了一个错误。 I'll make it simpler in example, but this also stands for your case when you have
new_dict[a][b]...[n]
.我会在示例中使其更简单,但是当您拥有
new_dict[a][b]...[n]
时,这也代表您的情况。
Here is how you probably are generating your dictionary:以下是您生成字典的方式:
lst = [0, 0, 0, 0, 0, 0, 0]
new_dict = []
for p in range(N):
new_dict[p] = lst
This however binds every new_dict[p]
, for p=0,...,N
to the same lst
, ie each new_dict[p]
value references the same instance of list
.然而,这
new_dict[p]
每个new_dict[p]
绑定到相同的lst
,因为p=0,...,N
,即每个new_dict[p]
值引用相同的list
实例。
You have to generate new list for each new_dict[p]
.您必须为每个
new_dict[p]
生成新列表。
Here is how you should generate it:以下是您应该如何生成它:
new_dict = {}
for p in range(N):
new_dict[p] = [0, 0, 0, 0, 0, 0, 0]
After your dictionary is populated, you can edit it with one line:填充字典后,您可以用一行对其进行编辑:
new_dict['a1']['RoadType'][0] = 5
Just update a2
separately:只需单独更新
a2
:
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)
Output:输出:
{'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]}}
Try this code : There is a small mistake in your code ,you update the index 0 of 'a2' and then assign the 'kk' that point the new_dict of 'a2'试试这个代码:你的代码有一个小错误,你更新'a2'的索引0,然后分配指向'a2'的new_dict的'kk'
Program :程序 :
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)
Output :输出 :
{'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]},
'a1': {'Road_Type': [0, 0, 0, 0, 0, 0, 0]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.