如何从Python上的打印值中获取特定值并从高到小排序

2024-10-02 10:31:23 发布

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

我正在尝试在我的项目中使用binance api,我想列出一些最受欢迎的项目,并将它们从高到低进行分类。我尝试了一些方法,但都没有成功

我只想打印“符号”“价格变动百分比”。 有没有办法得到这两个值

这是我的输出:

[
   {
      "symbol":"FIDABUSD",
      "priceChange":"0.41800000",
      "priceChangePercent":"6.375",
      "weightedAvgPrice":"6.95111809",
      "prevClosePrice":"6.54400000",
      "lastPrice":"6.97500000",
      "lastQty":"28.30000000",
      "bidPrice":"6.97400000",
      "bidQty":"74.80000000",
      "askPrice":"6.97900000",
      "askQty":"3.30000000",
      "openPrice":"6.55700000",
      "highPrice":"7.20000000",
      "lowPrice":"6.47700000",
      "volume":"354812.40000000",
      "quoteVolume":"2466342.89060000",
      "openTime":1633166019175,
      "closeTime":1633252419175,
      "firstId":78716,
      "lastId":88805,
      "count":10090
   },

   {
      "symbol":"FIDABNB",
      "priceChange":"0.00093000",
      "priceChangePercent":"6.008",
      "weightedAvgPrice":"0.01614960",
      "prevClosePrice":"0.01546000",
      "lastPrice":"0.01641000",
      "lastQty":"109.10000000",
      "bidPrice":"0.01643000",
      "bidQty":"97.50000000",
      "askPrice":"0.01649000",
      "askQty":"140.60000000",
      "openPrice":"0.01548000",
      "highPrice":"0.01663000",
      "lowPrice":"0.01533000",
      "volume":"75225.50000000",
      "quoteVolume":"1214.86161500",
      "openTime":1633166016671,
      "closeTime":1633252416671,
      "firstId":8400,
      "lastId":9840,
      "count":1441
   },
]

以下是我尝试过的:

class BinanceConnection:
def __init__(self, file):
    self.connect(file)

""" Creates Binance client """

def connect(self, file):
    lines = [line.rstrip('\n') for line in open(file)]
    key = lines[0]
    secret = lines[1]
    self.client = Client(key, secret)


if __name__ == '__main__':

connection = BinanceConnection(filename)
prices = connection.client.get_ticker()

print(prices)

Tags: 项目selfclientsymbolfilelineslastpricebidprice
2条回答

试试这个:

data = *your data*
newlist = list()
for item in data: 
    newlist.append({key:item[key] for key in ['symbol', 'priceChange']})

print(sorted(newlist, key=lambda k: k['priceChange'], reverse=True) )

见下文-a 1衬垫

data = [
   {
      "symbol":"FIDABUSD",
      "priceChange":"0.41800000",
      "priceChangePercent":"6.375",
      "weightedAvgPrice":"6.95111809",
      "prevClosePrice":"6.54400000",
      "lastPrice":"6.97500000",
      "lastQty":"28.30000000",
      "bidPrice":"6.97400000",
      "bidQty":"74.80000000",
      "askPrice":"6.97900000",
      "askQty":"3.30000000",
      "openPrice":"6.55700000",
      "highPrice":"7.20000000",
      "lowPrice":"6.47700000",
      "volume":"354812.40000000",
      "quoteVolume":"2466342.89060000",
      "openTime":1633166019175,
      "closeTime":1633252419175,
      "firstId":78716,
      "lastId":88805,
      "count":10090
   },

   {
      "symbol":"FIDABNB",
      "priceChange":"0.00093000",
      "priceChangePercent":"6.008",
      "weightedAvgPrice":"0.01614960",
      "prevClosePrice":"0.01546000",
      "lastPrice":"0.01641000",
      "lastQty":"109.10000000",
      "bidPrice":"0.01643000",
      "bidQty":"97.50000000",
      "askPrice":"0.01649000",
      "askQty":"140.60000000",
      "openPrice":"0.01548000",
      "highPrice":"0.01663000",
      "lowPrice":"0.01533000",
      "volume":"75225.50000000",
      "quoteVolume":"1214.86161500",
      "openTime":1633166016671,
      "closeTime":1633252416671,
      "firstId":8400,
      "lastId":9840,
      "count":1441
   }
]

data = sorted([{'symbol':x['symbol'],'priceChangePercent':x['priceChangePercent']} for x in data],key = lambda k: float(k['priceChangePercent']), reverse=True)
print(data)

输出

[{'symbol': 'FIDABUSD', 'priceChangePercent': '6.375'}, {'symbol': 'FIDABNB', 'priceChangePercent': '6.008'}]

相关问题 更多 >

    热门问题