【问题标题】:How to Select the Highest Attribute For a Selected Attribute in JSON Using Python如何使用 Python 为 JSON 中的选定属性选择最高属性
【发布时间】:2021-11-26 12:08:02
【问题描述】:

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

如何为唯一令牌地址选择最高 id ? 令牌地址是唯一的,但对于一个令牌地址,由于销售原因,有许多 id。 我需要获取令牌地址的最新 ID。然后我需要获取令牌地址的最大id的价格。

JSON 片段: “身份证”:908471, "token_add":"8p9LZH6VjfqQhMPoNuT6DyPZnBUje22mhanncdEzLWsv",

这是我的代码:

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'):
    floor_price = min(map(lambda x: x['price'], data))
    await message.channel.send("The Floor Price is: " + str(floor_price))

client.run(my_secret)

【问题讨论】:

    标签: python json list sorting discord


    【解决方案1】:

    使用地图和过滤器:

    max_ids = []
    unique_tokens = set(v["token_add"] for v in data)
    
    for unique_token in unique_tokens:
        max_id = max(
            map(
                lambda x: x["id"],
                list(
                    filter(
                        lambda x: x["token_add"] == unique_token,
                        data,
                    )
                ),
            )
        )
        max_ids.append({"token_add": unique_token, "max_id": max_id})
    

    返回(一些行):

    [ {'max_id': 833785,
      'token_add': 'G32GiuACw5savubWG454kYGcxgHfum4bi8HY4WaC5iKV'},
     {'max_id': 808390,
      'token_add': '6uZ3th4EjovR2X5RJf2SSY6dgTXifEmjyin7RSb5vVYv'},
     {'max_id': 1059873,
      'token_add': 'BZvoMxGMmAHqDLMaEZo7V5WW9P5HzVQBvCgDAbFFyScU'}]
    

    【讨论】:

    • 我需要为每个令牌地址提供该功能。它应该获取所有令牌地址并在 JSON 中找到最大的 id。不是针对一个唯一令牌,而是 JSON 中的所有令牌。然后我需要得到 id 最大的 token 的价格。
    • 按要求编辑。您可以根据max_id 属性获取价格。
    • 如何从 max_ids 获得最低价格?抱歉,我是 Python 新手。请帮我获取代码。
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多