【问题标题】:How to Select the Minimum Attribute in a JSON?如何选择 JSON 中的最小属性?
【发布时间】:2021-11-26 04:46:23
【问题描述】:

我正在开发一个不和谐的机器人来获取 Solanart 的底价。 我正在使用这个 JSON:https://qzlsklfacc.medianetwork.cloud/nft_for_sale?collection=cryptocavemen

如何在这个 JSON 文件中选择最低价格?

当用户每次输入 !floor 时,程序需要找到最低价格。

这是我的代码。一切正常,但我无法管理如何获得最低价格属性。

import discord
import os
import json
import urllib

my_secret = os.environ['TOKEN']
client = discord.Client()

with urllib.request.urlopen("https://qzlsklfacc.medianetwork.cloud/nft_for_sale? 
collection=cryptocavemen") as url:
data = json.loads(url.read().decode())


@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('!floor'):

    ??????????

    await message.channel.send("The Floor Price is: " + #floor price data )

client.run(my_secret)

需要你的帮助!谢谢!

【问题讨论】:

  • 将所有价格提取到一个列表中,然后使用 min(pricelist)?

标签: python json sorting discord


【解决方案1】:

不要将其视为 JSON 文件,一旦准备好为 json.loads,它只是一个字典列表,您可以在其中检查每个字典的 price 属性。

如果只想返回最低价格,可以结合mapmin函数在一行中获取结果:

floor_price = min(map(lambda x: x['price'], data))

【讨论】:

  • PS:我不确切知道 Discord 机器人流程是如何工作的,但也许您有兴趣从客户端事件中的 URL 读取,因此它总是在请求时更新.
猜你喜欢
  • 2021-11-26
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2023-03-11
  • 2010-10-29
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多