简体   繁体   English

如何在python中的字典中到达特定的键值对?

[英]How to reach a particular key value pair inside a dictionary in python?

I have json document as below.我有如下 json 文档。 I would like to update all "points" to type integer.我想更新所有“点”以输入整数。 I'm familair with how to typecast.我熟悉如何打字。 However I'm not able to reach to each indiviaul key/value pair inside the key "score".但是,我无法访问键“分数”内的每个单独的键/值对。 Would you please help me guide with the concept here你能帮我指导一下这里的概念吗

{"playerID": "123",
"score": [{"date": "1/1/2019", 
           "points": "10", 
            "somekey": "somevalue"
           }, 
           {"date": "1/1/2018", 
           "points": "100", 
            "somekey": "valuexyz"
           }]
}

I tried to read the json data into variable called "data" data.get("score") returned a "list" of items item(0) gets me entire record - date, points, somekey.我试图将 json 数据读入名为“data”的变量中 data.get("score") 返回了一个项目的“列表” item(0) 让我得到完整的记录 - 日期、点数、somekey。 I'm not able to get to a specific key called points.我无法获得称为点的特定关键。

Should I onceagain convert my list to dictionary and then iterate to get to points?我应该再次将我的列表转换为字典,然后迭代以获得点数吗? Isn't there any other way没有其他办法吗

data = {"playerID": "123",
"score": [{"date": "1/1/2019", 
       "points": "10", 
        "somekey": "somevalue"
       }, 
       {"date": "1/1/2018", 
       "points": "100", 
        "somekey": "valuexyz"
       }]
}
dlist = data.get("score") #returns list
for x in data['score']:
    x['points'] = int(x['points'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM