我目前正在对Steam Web API进行API调用,该API获得了一个类似于以下内容的json响应:
{
"response": {
"globalstats": {
"heist_success": {
"total": "58932654920",
"history": [
{
"date": 1486252800,
"total": "696574"
},
{
"date": 1486339200,
"total": "357344"
},
{
"date": 1486425600,
"total": "356800"
},
{
"date": 1486512000,
"total": "311056"
}
]
}
},
"result": 1
}
日期以Unix时间戳记,总数为一个数量。我想做的是根据日期和时间中的值而不是名称来创建字典。我尝试使用以下内容:
dict = dict(data['response']['globalstats']['heist_success']['history'])
但这只是创建了一个“日期”“总计”的字典。
如何仅根据值创建字典?
最佳答案
您可以获取值并从中提取字典,
这就是你可能会做的
码
d = data['response']['globalstats']['heist_success']['history']
result_dict = dict((i["date"],i["total"]) for i in d)
如果使用python 2.7或更高版本,也可以使用
dict comprehension
result_dict = {i["date"]:i["total"] for i in d}
输出量
{1486252800: '696574',
1486339200: '357344',
1486425600: '356800',
1486512000: '311056'}