【问题标题】:Why size changes while converting list to dictionary in Python? [duplicate]为什么在 Python 中将列表转换为字典时大小会发生变化? [复制]
【发布时间】:2016-11-04 05:54:33
【问题描述】:

基本上我有列表(整个棕色语料库)。

dic1=brown.words()
print 'Total size of Brown Corpus :'
print len(dic1)

输出是:

Total size of Brown Corpus :
1161192

我将该列表转换为字典,使所有值都为 2 并检查了大小。

dic=dict((k,2) for k in dic1)
print 'Size of the dict:'
print len(dic)

得到了这个输出:

Size of the dict:
56057

字典包含:(key:2)。因此,如果列表中没有 1161192 个单词,则字典中也应该有相同数量的条目。为什么尺寸会发生变化?

【问题讨论】:

    标签: python list dictionary


    【解决方案1】:

    字典只存储不同的结果,因此当您将列表转换为字典时,您会丢失所有重复的单词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 2018-04-14
      • 1970-01-01
      • 2019-11-16
      • 2012-05-21
      • 2018-05-01
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多