【问题标题】:How can I remove duplicate elements in a JSON file?如何删除 JSON 文件中的重复元素?
【发布时间】:2021-12-09 17:20:51
【问题描述】:

我试图编写一个程序,以 JSON 文件的形式向我输出信息,但我遇到了这样一个事实,即我使用 "exchange_id" 参数复制了信息,但使用了其他信息。我希望向我显示一次具有此值的信息。我该怎么做?

JSON 文件示例:

{
            "exchange_name": "Huobi Global",
            "market_url": "https://www.huobi.com/en-us/exchange/eth_btc",
            "price": 48637.97781883441,
            "last_update": "2021-12-09T17:11:53.000Z",
            "exchange_id": 102
        },
{
            "exchange_name": "Huobi Global",
            "market_url": "https://www.huobi.com/en-us/exchange/xrp_btc",
            "price": 48656.10607332287,
            "last_update": "2021-12-09T17:10:53.000Z",
            "exchange_id": 102
        },

我的代码:

json_data = response['data']
            pairs = []
            out_object = {}

            exchangeIds = {102,311,200,302,521,433,482,406,42,400}

            for pair in json_data["marketPairs"]:
                if pair['exchangeId'] in set(exchangeIds):
                    pairs.append({

                        "exchange_name": pair["exchangeName"],
                        "market_url": pair["marketUrl"],
                        "price": pair["price"],
                        "last_update" : pair["lastUpdated"],
                        "exchange_id": pair["exchangeId"]

                        })

                    out_object["name_of_coin"] = json_data["name"]
                    out_object["marketPairs"] = pairs
                    out_object["pairs"] = json_data["numMarketPairs"]
 
                    name = json_data["name"]

                    save_path = '.\swapMarketCap\coins'
                    file_name = f'{name}'
                    path_to_coin = os.path.join(save_path, file_name +".json")

【问题讨论】:

  • 您可以在创建其 json 条目后从 exchangeIds 中删除该元素。在为 exchangeIds = 102 创建 json 条目后,只需从集合中删除 102。
  • 在这个例子中期望的输出是什么?
  • 我想只输出一件事,像这样:
  • "exchange_name": "Huobi Global", "market_url": "huobi.com/en-us/exchange/eth_btc", "price": 48637.97781883441, "last_update": "2021-12-09T17:11:53.000Z", “exchange_id”:102

标签: python json


【解决方案1】:

在没有更多细节的情况下,这里有一个您可以使用的模式。这确实声称要重写您的代码,并且还会做出某些假设(在原始问题中没有详细信息的情况下)。但是,它应该可以帮助您:

response = {'data':
            {'marketPairs': [{
                "exchange_name": "Huobi Global",
                "market_url": "https://www.huobi.com/en-us/exchange/eth_btc",
                "price": 48637.97781883441,
                "last_update": "2021-12-09T17:11:53.000Z",
                "exchange_id": 102
            },
                {
                "exchange_name": "Huobi Global",
                "market_url": "https://www.huobi.com/en-us/exchange/xrp_btc",
                "price": 48656.10607332287,
                "last_update": "2021-12-09T17:10:53.000Z",
                "exchange_id": 102
            }
            ]}}

exchangeIds = {102, 311, 200, 302, 521, 433, 482, 406, 42, 400}

output = {'data': {'marketPairs': []}}

for d in response['data']['marketPairs']:
    if (id := d.get('exchange_id', None)):
        if id in exchangeIds:
            output['data']['marketPairs'].append(d)
            exchangeIds.remove(id)

print(output)

因此,基本思想是您有一组您感兴趣的 ID。一旦您找到其中的一个,请获取当前字典的引用并将其添加到新字典(输出)。然后,至关重要的是,从集合中删除之前处理​​过的 ID。

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 2010-09-12
    • 2022-08-22
    • 2021-03-15
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多