Python: How to update value of key value pair in nested dictionary?

后端 未结 9 1074
广开言路
广开言路 2021-01-21 20:25

i am trying to make an inversed document index, therefore i need to know from all unique words in a collection in which doc they occur and how often.

i have used this an

9条回答
  •  天命终不由人
    2021-01-21 21:18

    In the AutoVivification class, you define

    value = self[item] = type(self)()
    return value
    

    which returns an instance of self, which is an AutoVivification in that context. The error becomes then clear.

    Are you sure you want to return an AutoVivification on any missing key query? From the code, I would assume you want to return a normal dictionary with string key and int values.

    By the way, maybe you would be interested in the defaultdict class.

提交回复
热议问题