[英]How to store values from a dictionary into another dictionary
我想在遍歷所有項目時將現有字典中的值存儲到另一個字典中? 如何才能做到這一點? 我試圖使用這個訪問所有項目:
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])
for 循環中的上述語句會引發錯誤。 如果我嘗試打印值而不是將它們存儲在臨時字典中,它會正確顯示它們。 我怎樣才能做到這一點? 提前致謝。
試試下面的代碼
temp = {}
for responseDesc in res :
if responseDesc =='responseDesc':
temp = res[responseDesc]
如果您只是想以最簡單的方式復制,請使用temp=dict(res)
如果您正在考慮遍歷 json 的所有嵌套層,您可以:
如果要遍歷 responseDesc 數組: for responseDesc in res['responseDesc']:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.