如何获得N个最高数字?

时间:2021-01-14 08:11:10

标签: python bots pycrypto cryptoapi

我有这个代码,我必须得到一个答案,从'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))

2 个答案:

答案 0 :(得分:0)

from heapq import nlargest

print(nlargest(n, currencies))

答案 1 :(得分:0)

因为您已将货币值存储在列表中。您可以先按降序排列: sorted = currencies.sort(reverse=True) 那么下面将从您的列表中给出您的 N 最高值。 print(sorted[-N:])

相关问题