我有密码:
r = requests.get(my_url)
d = sorted(r.json().values(), key=lambda x: x['players'], reverse=True)[0:5]
``` `d` 是:
[{'gamemode': 'roleplay',
'lang': 'ru',
'maxplayers': 5000,
'name': '[RolePlay][Voice] GTA5RP.COM | DownTown | gta5rp.com/discord',
'peak': 1716,
'players': 1662,
'url': 'https://gta5rp.com/'},
{'gamemode': 'roleplay',
'lang': 'ru',
'maxplayers': 5000,
'name': '[RolePlay][Voice] GTA5RP.COM | VineWood | gta5rp.com/discord',
'peak': 1578,
'players': 1568,
'url': 'https://gta5rp.com/'},
{'gamemode': 'roleplay',
'lang': 'ru',
'maxplayers': 5000,
'name': '[RolePlay][Voice] GTA5RP.COM | Eclipse | gta5rp.com/discord',
'peak': 1489,
'players': 1459,
'url': 'https://gta5rp.com/'},
{'gamemode': 'roleplay',
'lang': 'ru',
'maxplayers': 5000,
'name': '[RolePlay][Voice] GTA5RP.COM | StrawBerry | gta5rp.com/discord',
'peak': 1397,
'players': 1389,
'url': 'https://gta5rp.com/'},
{'gamemode': 'roleplay',
'lang': 'ru',
'maxplayers': 3500,
'name': '[RolePlay][Voice] GTA5RP.COM | Sunrise | gta5rp.com/discord [1.1]',
'peak': 1337,
'players': 1323,
'url': 'https://gta5rp.com/'}]
如何使用 `for` 这样地?
print('Name: ', d["name"]... etc
2条答案
按热度按时间cngwdvgl1#
n7taea2i2#
d是一个字典列表,因此您可以在该列表上迭代以获取每个dict并格式化输出:
结果: