【发布时间】:2021-01-14 08:11:10
【问题描述】:
我有这个代码,我必须得到一个答案,它会从 'r' 变量中返回每个 percent_change_24h 的 10 个最高数字。我应该使用什么方法?我见过 max 方法,但那个方法只返回一个值(最高肯定,但只有一个)
url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
params={
'start':'1',
'limit':'100',
'convert':'USD'
}
headers={
'Accepts':'applications/json',
'X-CMC_PRO_API_KEY':'b8ee0ea1-ae9b-44ab-9132-02e6e5430eb1'
}
#data= requests.get(url=url,headers=headers,params=params).json()
#pprint(data)`
r= requests.get(url=url,headers=headers,params=params).json()
currencies=[]
for currency in r['data']:
if currency['quote']['USD']['percent_change_24h']>1:
currencies.append(
currency['symbol']
)
pprint(max(currencies))
【问题讨论】:
-
您可以在
currencies列表上使用sort()或sorted()函数,然后打印此列表中的前10 项。 -
print(sorted(currencies, reverse=True)[:10])
标签: python bots pycrypto cryptoapi