试图获取字典中的所有值
Trying to get all values in a dict
我正在从 PokeAPI 获取数据,但如果该属性有多个值,例如 abilities
和 stats
,我只获取最后一个。
dic_abi = {} #logic for abilities
for i in lista['abilities']:
dic_abi = i['ability']['name']
contexto = {
#"pokemons": dic,
'ID': lista['id'],
'Nome': lista['name'],
'Tipo': lista['types'],
'Peso': lista['weight'],
'Altura': lista['height'],
'Habilidades': dic_abi, #more than one value
'Estatisticas': lista['stats'] #more than one value
}
JSON 我正在尝试获取:
{"abilities":[{"ability":{"name":"static","url":"https://pokeapi.co/api/v2/ability/9/"},"is_hidden":false,"slot":1},{"ability":{"name":"lightning-rod" ........
很难理解您要实现的目标。如果你想在 lista['abilities']
中列出能力的名称,那么你可以做列表理解而不是 for 循环
dic_abi = [i['ability']['name'] for i in lista['abilities']]
如果你想创建一个字典,其中能力名称为 key
,能力名称为 value
(可能这就是你想要的),那么你应该这样做:
for i in lista['abilities']:
dic_abi[i['ability']['name']] = i['ability']
或听写理解:
dic_abi = {i['ability']['name']: i['ability'] for i in lista['abilities']}
我正在从 PokeAPI 获取数据,但如果该属性有多个值,例如 abilities
和 stats
,我只获取最后一个。
dic_abi = {} #logic for abilities
for i in lista['abilities']:
dic_abi = i['ability']['name']
contexto = {
#"pokemons": dic,
'ID': lista['id'],
'Nome': lista['name'],
'Tipo': lista['types'],
'Peso': lista['weight'],
'Altura': lista['height'],
'Habilidades': dic_abi, #more than one value
'Estatisticas': lista['stats'] #more than one value
}
JSON 我正在尝试获取:
{"abilities":[{"ability":{"name":"static","url":"https://pokeapi.co/api/v2/ability/9/"},"is_hidden":false,"slot":1},{"ability":{"name":"lightning-rod" ........
很难理解您要实现的目标。如果你想在 lista['abilities']
中列出能力的名称,那么你可以做列表理解而不是 for 循环
dic_abi = [i['ability']['name'] for i in lista['abilities']]
如果你想创建一个字典,其中能力名称为 key
,能力名称为 value
(可能这就是你想要的),那么你应该这样做:
for i in lista['abilities']:
dic_abi[i['ability']['name']] = i['ability']
或听写理解:
dic_abi = {i['ability']['name']: i['ability'] for i in lista['abilities']}