Hej chłopaki Mam listę słowników, które mają znaczniki czasu, które chcę oddzielić się w każdej iteracji.

Więc obecnie wygląd wygląda na to:

{
    "taco.tuesday": [
        {
            "timestamp": "2021-02-11 02:18:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:19:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:20:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:21:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:22:00-05:00",
            "cost": 0
        }
    ],
    "macoroni.mondays": [
        {
            "timestamp": "2021-02-11 02:18:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:19:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:20:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:21:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:22:00-05:00",
            "cost": 0
        }
    ],
    "salmon.sundays": [
        {
            "timestamp": "2021-02-11 02:18:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:19:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:20:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:21:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:22:00-05:00",
            "cost": 0
        }
    ],
    "whatever.wednesday": [
        {
            "timestamp": "2021-02-11 02:18:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:19:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:20:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:21:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-11 02:22:00-05:00",
            "cost": 0
        }
    ]
}

Ale to, czego chcę, to moje wyniki, aby wyglądać tak na każdą minutę:

[{
    "taco.tuesday": {
        "timestamp": "2021-02-11 01:38:00-05:00",
        "cost": 0
    },
    "macoroni.mondays": {
        "timestamp": "2021-02-11 01:38:00-05:00",
        "cost": 0
    },
    "salmon.sundays": {
        "timestamp": "2021-02-11 01:38:00-05:00",
        "cost": 0
    },
    "whatever.wednesday": {
        "timestamp": "2021-02-11 01:38:00-05:00",
        "cost": 0
    }
}],

[{
    "taco.tuesday": {
        "timestamp": "2021-02-11 01:39:00-05:00",
        "cost": 0
    },
    "macoroni.mondays": {
        "timestamp": "2021-02-11 01:39:00-05:00",
        "cost": 0
    },
    "salmon.sundays": {
        "timestamp": "2021-02-11 01:39:00-05:00",
        "cost": 0
    },
    "whatever.wednesday": {
        "timestamp": "2021-02-11 01:39:00-05:00",
        "cost": 0
    }
}],

[{
    "taco.tuesday": {
        "timestamp": "2021-02-11 01:40:00-05:00",
        "cost": 0
    },
    "macoroni.mondays": {
        "timestamp": "2021-02-11 01:40:00-05:00",
        "cost": 0
    },
    "salmon.sundays": {
        "timestamp": "2021-02-11 01:40:00-05:00",
        "cost": 0
    },
    "whatever.wednesday": {
        "timestamp": "2021-02-11 01:40:00-05:00",
        "cost": 0
    }
}]

Oto, co zrobiłem do tej pory, aby uzyskać wyniki, które chciałem:

e = defaultdict(dict)
for i in range(len(ts_results)):
    for key, value in tables.items():
        e[key]['timesamp'] = str(ts_results[i])
        e[key]['cost'] = 0

Jaki jest najlepszy sposób na osiągnięcie tego rozwiązania?

0
Malcode 11 luty 2021, 10:48

2 odpowiedzi

Najlepsza odpowiedź

Jest to oparte na odpowiedziach Ángel Igualada, ale koryguje, jak tworzy zagnieżdżone słowniki.

dict_by_time = {}
# for over your dict (called d here)
for k,v in d.items(): 
    for record in v:
        t = record["timestamp"]
        d = dict_by_time.get(t, {})
        d[k] = record
        dict_by_time[t] = d

result = list(dict_by_time.values())
1
Barmar 11 luty 2021, 08:27

Bez użycia dodatkowych bibliotek, możesz zrobić coś w rodzaju:

dict_by_time = {}
# for over your dict (called d here)
dict_by_time = {}
for k,v in d.items():
    for record in v:
        t = record["timestamp"]
        if t not in dict_by_time:
            dict_by_time[t] = {}
        dict_by_time[t].update({k:record})

Daje to dict indeksowany przez ciąg znaków, jeśli chcesz tylko wartości, możesz użyć:

list(dict_by_time.values())
1
Ángel Igualada 11 luty 2021, 08:30