[英]How to store values from a dictionary into another dictionary
I want to store the values in an existing dictionary into another dictionary while traversing through all the items?我想在遍历所有项目时将现有字典中的值存储到另一个字典中? How can this be done?
如何才能做到这一点? I have tried to access all items using this:
我试图使用这个访问所有项目:
res = {
"responseCode": 0,
"responseDesc": [
{
"city_id": 1,
"city_name": "Mumbai",
"description": "Mumbai",
"latitude": 18.987807,
"longitude": 72.836447,
"total_trips": 0
}
]
}
temp = {}
for responseDesc in res :
temp = (res[responseDesc])
The above statement in for loop throws an error. for 循环中的上述语句会引发错误。 If I try to print the values instead of storing them in the temp dictionary it shows them correctly.
如果我尝试打印值而不是将它们存储在临时字典中,它会正确显示它们。 How can I achieve this?
我怎样才能做到这一点? Thanks in advance.
提前致谢。
try below code试试下面的代码
temp = {}
for responseDesc in res :
if responseDesc =='responseDesc':
temp = res[responseDesc]
If you are just looking to take a copy in the easiest possible way, use temp=dict(res)
如果您只是想以最简单的方式复制,请使用
temp=dict(res)
If you are looking at iterating through all the nested layers of the json you can:如果您正在考虑遍历 json 的所有嵌套层,您可以:
If you want to iterate through the responseDesc array: for responseDesc in res['responseDesc']:
如果要遍历 responseDesc 数组:
for responseDesc in res['responseDesc']:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.