【问题标题】:Python interior dictionary default values [duplicate]Python内部字典默认值[重复]
【发布时间】:2017-01-27 17:48:04
【问题描述】:

我正在尝试制作一个字典字典,其中内部字典中的默认值的默认值为0.0

例如,它可能看起来像这样:{'Jack':{'foo':0.0, 'bar':1.4, ...}, 'Mike': {'foo':6.2, ...} }

我想将所有单词初始化为 0.0 值,这样我就可以简单地说:

for word in document:
    my_dictionary['Jack'][word] += 1.4

我尝试过使用defaultdict(dict)Counter,但我不能确定这些策略是否正确。

有什么想法吗?

【问题讨论】:

    标签: python dictionary counter defaultdict


    【解决方案1】:

    试试这个:

    my_dictionary = defautltdict(lambda: defaultdict(float))
    

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 2016-06-25
      • 2017-09-06
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多