从API需求中搜索字典内的列表

2024-09-30 12:14:58 发布

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

我正在尝试查询一个包含列表和另一个词典的词典。我正在使用卡片组API,并希望提取值

drawCard='https://deckofcardsapi.com/api/deck/vx58tedq5moe/draw/?count=1'

response = requests.get(drawCard)
getValue = json.loads(response.text)
value= (getValue['cards'])
print (getValue)
print("")
print("")
print (value)
card= (getValue['cards'](''))

这是我打印getValue时得到的结果

{'deck_id': 'vx58tedq5moe', 'success': True, 'cards': [{'suit': 'SPADES', 'code': '0S', 'value': '10', 'images': {'png': 'https://deckofcardsapi.com/static/img/0S.png', 'svg': 'https://deckofcardsapi.com/static/img/0S.svg'}, 'image': 'https://deckofcardsapi.com/static/img/0S.png'}], 'remaining': 44}

我使用getValue['cards']将它缩小到这个范围,但不能再进一步了

[{'suit': 'SPADES', 'code': '0S', 'value': '10', 'images': {'png': 'https://deckofcardsapi.com/static/img/0S.png', 'svg': 'https://deckofcardsapi.com/static/img/0S.svg'}, 'image': 'https://deckofcardsapi.com/static/img/0S.png'}]

我想从值中取10


Tags: httpssvgcomimgpngvaluestatic词典
2条回答

你可以像这样下到多个层次:

value= (getValue['cards']['value'])

BC“cards”是列表的键,“value”是列表中值为10的值的键

getValue['cards']的值是一个列表,因此首先要访问list元素getValue['cards'][0],然后可以使用

getValue['cards'][0]['value']

如果你想从卡片列表中的所有卡片中得到所有的值,你可以使用列表理解来做如下的事情

[c['value'] for c in getValue['cards']]

相关问题 更多 >

    热门问题