简体   繁体   English

Python 使用来自 N 个具有相同条目但值不同的字典的值列表制作主字典

[英]Python make master dictonary with list of values from N dictionaries with identical entries but different values

I have a server that spits out status updates five times a second.我有一台服务器每秒吐出五次状态更新。 After some manipulation, my program creates a JSON dict with roughly 3000 entries.经过一些操作,我的程序创建了一个包含大约 3000 个条目的 JSON 字典。 This is received every 200ms, so the JSON is exactly the same in terms of its entries, but the values change.这是每 200 毫秒接收一次,因此 JSON 的条目完全相同,但值会发生变化。 I need to test some inputs and see exactly which entries change, for how long, etc.. This is a sample of the structure of the JSON dict I get:我需要测试一些输入,看看哪些条目发生了变化,持续了多长时间等等。这是我得到的 JSON dict 的结构示例:

 {
  "start-flag": 123456789,
  "packet_length": 3012,
  "id": 1253,
  "Subsystem_1": {
    "mode": 4,
    "is_active": 0,
    ...
    "voltage-on": 1},
  "Subsystem_2": {
    "local_temp": 25,
    "temp_error": 0,
    ...
    "collective_status_error": 0},
  ...
  "irbh_system_status": 0,
  "end-flag": 987654321}

You get the idea.你明白了。 But the values change often, and I would like to somehow record all the received dictionaries over a timeframe and make a master that looks like this:但是值经常变化,我想以某种方式记录一段时间内收到的所有字典,并制作一个看起来像这样的主:

 {
  "start-flag": [123456789, 123456789, 123456789, 123456789]
  "packet_length": [3012, 3012, 3012, 3012]
  "id": [1253, 1442, 2345, 2355]
  "Subsystem_1": {
    "mode": [4, 1, 1, 4]
    "is_active": [0, 1, 1, 0]
    ...
    "voltage-on": [1, 2.4, 2.4, 1]},
  "Subsystem_2": {
    "local_temp": [25, 28, 35, 100],
    "temp_error": [0, 0, 0, 1],
    ...
    "collective_status_error": [0, 0, 0, 1]},
  ...
  "irbh_system_status": [0, 0, 0, 0],
  "end-flag": [987654321, 987654321, 987654321, 987654321]}

This would be 4 different JSON dicts added together to make a master.这将是 4 个不同的 JSON dicts 加在一起构成一个主控。 Entries are the same, but the values change over time, and I would like to see them.条目是相同的,但值会随着时间而变化,我想看看它们。 I'd probably need this for ~600 dicts as i receive them quite often and would need a 2min ish sample (or more).我可能需要大约 600 个字典,因为我经常收到它们,并且需要 2 分钟的样本(或更多)。

What's the best way to make this master dict while running?运行时制作这个主命令的最佳方法是什么? Ie a function that takes a dict when called and if it's the first makes it the master dict, but if it is not the first, then simply adds all values of all entries (including those of nested-dictionaries) to the master, and makes all values in the master lists?即一个 function 在被调用时接受一个字典,如果它是第一个使其成为主字典,但如果它不是第一个,则只需将所有条目的所有值(包括嵌套字典的值)添加到主控,并使主列表中的所有值? The values of the entries of the dictionaries received are always either int or float.接收到的字典条目的值始终是 int 或 float。

EDIT: I forgot to include this, as I just discovered this bug trying to solve this myself.编辑:我忘了包括这个,因为我刚刚发现这个错误试图自己解决这个问题。 Some of the nested dictionaries have the same entry names.一些嵌套字典具有相同的条目名称。 In the above example this could be shown with both Subsystem_1 and Subsystem_2 having the "mode" and "is_active" entry, I just forgot to include such a field.在上面的示例中,可以使用具有“mode”和“is_active”条目的 Subsystem_1 和 Subsystem_2 来显示,我只是忘记包含这样一个字段。 These are two different entries for two different subsystems (thus why they're in different nested dicts), but have the same entry name in their respective dictionary.这是两个不同子系统的两个不同条目(因此它们位于不同的嵌套字典中),但在它们各自的字典中具有相同的条目名称。 Sadly I can't edit these names as it's being sent from a black box computer I can only read from.遗憾的是,我无法编辑这些名称,因为它是从我只能读取的黑匣子计算机发送的。

Leniks' solution works if I add a如果我添加 Leniks 的解决方案

d = my_stream_json_dict

# all the names of the first level nested dicts,
# assuming no second level nested dicts exist.
headers = ['Subsystem_1', 'Subsystem_2', ....]
for a in headers:
    for k,v in d[a].items() :
        master[k].append(v)

but doesn't take multiple nested dicts having the same entry name into account (since I didn't mention it, my bad,), and if the same entry exists in multiple nested dicts.但不考虑具有相同条目名称的多个嵌套字典(因为我没有提到它,我的错,),并且如果相同的条目存在于多个嵌套字典中。 it's populated multiple times.它被多次填充。

If the modified solution is ran only a single time with only one dictionary, the top level entries are not considered (to be honest, that's not an issue for me as only the nested dictionaries (subsystems) matter), but although every entry should have only one value as only 1 dictionary was provided, entries that share the same name over N nested dicts have N values in each nested dict entry如果修改后的解决方案仅使用一个字典运行一次,则不考虑顶级条目(老实说,这对我来说不是问题,因为只有嵌套字典(子系统)很重要),但是尽管每个条目都应该有只提供了一个值,因为只提供了 1 个字典,在 N 个嵌套字典中共享相同名称的条目在每个嵌套字典条目中都有 N 个值

Try this:尝试这个:

from collections import defaultdict

master = defaultdict(list)
for d in received_dictionaries :  # this is your data from the web
    for k,v in d.items() :
        master[k].append(v)

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

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