从API返回的列表中获取值

2024-04-28 21:23:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力从API调用返回的列表中提取某个值。以下是列表(缩短版本):

[{'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0, ...}]

返回方式:

client.Position.Position_get(filter=json.dumps({'symbol': 'XBTUSD'})).result()

我希望得到'avgEntryPrice'作为一个变量提取,提前感谢各位!你知道吗


Tags: 版本clientapijson列表get方式position
1条回答
网友
1楼 · 发布于 2024-04-28 21:23:45

您将获得如下值:

list = [{'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0, ...}]
list[0]["avgEntryPrice"]

因为列表只有字典作为它的项。因此,您需要访问列表的第一项,然后使用字典中的键访问所需的值。你知道吗

如果你有多个字典,你可以这样做:

list = [{'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0}, {'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0}, {'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0}, {'account': 231584,'simpleCost': 0.0, 'simpleValue': 0.0, 'avgEntryPrice': 0.0}]
avgEntryPrices = [item["avgEntryPrice"] for item in list]

这将返回列表中每个词典的avgEntryPrice。你知道吗

相关问题 更多 >