简体   繁体   English

组合包含 Python 中对象列表的字典

[英]Combining dictionaries that contain a list of objects in Python

I'm trying to combine two dictionaries (actually JSON).我正在尝试合并两个词典(实际上是 JSON)。 Essentially, the API I'm using only give the three newest values, so I want to combine those with the data I already have to create a longer history.本质上,我使用的 API 只提供了三个最新值,所以我想将它们与我已经拥有的数据结合起来以创建更长的历史记录。

Here is what I tried:这是我尝试过的:

old = {
    "price_history": [
    {
        "date": "3/13",
        "best_buy_price": 0,
        "best_sell_price": 742
    },
    {
        "date": "3/12",
        "best_buy_price": 0,
        "best_sell_price": 463
    },
    {
        "date": "3/11",
        "best_buy_price": 0,
        "best_sell_price": 445
    },
]}

new = {
    "price_history": [
    {
        "date": "3/14",
        "best_buy_price": 0,
        "best_sell_price": 1000
    },
    {
        "date": "3/13",
        "best_buy_price": 0,
        "best_sell_price": 742
    },
    {
        "date": "3/12",
        "best_buy_price": 0,
        "best_sell_price": 463
    },
]}

price_history = {**old, **new}

However, the output of this ends up being:但是,最终的 output 是:

    {
        "date": "3/14",
        "best_buy_price": 0,
        "best_sell_price": 1000
    },
    {
        "date": "3/13",
        "best_buy_price": 0,
        "best_sell_price": 742
    },
    {
        "date": "3/12",
        "best_buy_price": 0,
        "best_sell_price": 463
    },

I'm trying to get something like this:我想得到这样的东西:

    {
        "date": "3/14",
        "best_buy_price": 0,
        "best_sell_price": 1000
    },
    {
        "date": "3/13",
        "best_buy_price": 0,
        "best_sell_price": 742
    },
    {
        "date": "3/12",
        "best_buy_price": 0,
        "best_sell_price": 463
    },
    {
        "date": "3/11",
        "best_buy_price": 0,
        "best_sell_price": 445
    },

You can try something like this:你可以尝试这样的事情:

from datetime import datetime
def extract(v):
    return (v["date"], v["best_buy_price"], v["best_sell_price"])
temp = sorted(set(extract(v) for v in new["price_history"]) | set(extract(v) for v in old["price_history"]), key=lambda x: datetime.strptime(x[0], "%m/%d"), reverse=True)
price_history = {"price_history": [{"date": v[0], "best_buy_price": v[1], "best_sell_price": v[2]} for v in temp]}

More generally (ie more than just 3 keys in the object), you can do something like this (assuming new["price_history"] is non-empty)):更一般地(即对象中不止 3 个键),你可以做这样的事情(假设new["price_history"]是非空的):

from datetime import datetime
keys = sorted(new["price_history"][0])
date_index = keys.index("date")
def extract(val):
    return tuple(val[k] for k in keys)
temp = sorted(set(extract(v) for v in new["price_history"]) | set(extract(v) for v in old["price_history"]), key=lambda x: datetime.strptime(x[date_index], "%m/%d"), reverse=True)
price_history = {"price_history": [{keys[i]: v[i] for i in range(len(keys))} for v in temp]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM