简体   繁体   English

在 Python 的嵌套字典中更新计数器

[英]Updating counters in a nested dictionary in Python

I am using an empty results dictionary with initial values 0 which I would like to update upon occurrence once going through my source data.我正在使用一个初始值为 0 的空结果字典,一旦通过我的源数据,我想在出现时对其进行更新。 The Dictionary looks like this:字典看起来像这样:

{
  "Group1": {
    "1": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "2": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "3": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "4": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "5": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "6": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "not_specified": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    }
  },
  "Group2": {
    "1": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "2": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "3": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "4": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "5": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "6": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    },
    "not_specified": {
      "fr": {
        "answered": 0,
        "unanswered": 0
      },
      "nl": {
        "answered": 0,
        "unanswered": 0
      },
      "not_specified": {
        "answered": 0,
        "unanswered": 0
      }
    }
  }
}dic

When I try to update just a given counter though like:当我尝试仅更新给定的计数器时,例如:

myDic['Group1']['1']['nl']['answered']=myDic['Group1']['1']['nl']['answered']+10
myDic['Group2']['1']['nl']['answered']=myDic['Group2']['1']['nl']['unanswered']+100

I get absolutely all counters updated in the whole dictionary with the same values.我得到了在整个字典中使用相同值更新的所有计数器。 What is the reason for that?这是什么原因?

Your current code is valid, but looks like your {"answered": 0, "unanswered": 0} dict is a reference to the same object, so when you've change the value in one it changes everywhere.您当前的代码是有效的,但看起来您的{"answered": 0, "unanswered": 0} dict 是对同一个 object 的引用,因此当您更改其中的值时,它会随处更改。

Check if id(myDic['Group1']['1']['nl']) == id(myDic['Group1']['1']['fr']) ?检查id(myDic['Group1']['1']['nl']) == id(myDic['Group1']['1']['fr'])是否? If so, you need to use from copy import deepcopy when created these dicts.如果是这样,您需要在创建这些字典时使用from copy import deepcopy

This code smells but makes clear overview:这段代码有异味,但可以清楚地概述:

from copy import deepcopy

original = deepcopy(myDic['Group1']['1']['nl'])
original['answered'] += 10
myDic['Group1']['1']['nl'] = original

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

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