访问字典中的值

时间:2014-11-27 05:04:44

标签: python-3.x

这是我的字典:

  

{' request':[YearCount(year = 2005,count = 646179),YearCount(year = 2006,count = 677820),YearCount(year = 2007,count = 697645),YearCount(year = 2008,count = 795265)],'徘徊':[YearCount(年= 2005,计数= 83769),YearCount(年= 2006,计数= 87688),YearCount(年= 2007,计数= 108634) ),YearCount(年= 2008,计数= 171015)],'机场':[YearCount(年= 2007,计数= 175702),YearCount(年= 2008,计数= 173294)]}

我需要帮助找出如何访问YearCount - 计数值。因为我试图找到每个单词的字母频率,例如'请求','漫步'和' airport'。我算了总数 每个字母出现在输入数据集的所有单词中。然后将该数除以总数 所有单词中的字母数。

1 个答案:

答案 0 :(得分:0)

如果这是您的字典,那么您可以通过以下方式访问YearCount个对象列表:

objects = my_dict['request']

然后,您可以遍历列表并访问.count值:

for year_count in objects:
    print(year_count.count)

您还可以添加它们以获取该字词的总计数:

total = 0
for year_count in objects:
    total += year_count.count
print(total)

要显示您可以执行的所有单词出现:

for word, year_counts in my_dict.items():
    total = 0
    for year_count in year_counts:
        total += year_count.count
    print(word, total)