Python 3.x 访问字典中的值

Python 3.x 访问字典中的值,python-3.x,Python 3.x,这是我的字典: {'request':[YearCount year=2005,count=646179,YearCount year=2006,count=677820,YearCount year=2007,count=697645,YearCount year=2008,count=795265],'Wanderd':[YearCount year=2005,count=83769,YearCount year=2006,count=87688,YearCount year=2007,co

这是我的字典:

{'request':[YearCount year=2005,count=646179,YearCount year=2006,count=677820,YearCount year=2007,count=697645,YearCount year=2008,count=795265],'Wanderd':[YearCount year=2005,count=83769,YearCount year=2006,count=87688,YearCount year=2007,count=108634,YearCount year=2008,count=171015],'airport':[YearCount year=2007,count=175702,YearCount year=2008,count=173294]]

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

如果这是您的字典,则您可以通过以下操作访问YearCount对象列表:

objects = my_dict['request']
然后可以遍历列表并访问.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)

而不是:objects=my_dict['request']一般来说,我如何访问计数而不声明特定的键,因为我的字典不会每次都与此结构完全相同time@Adam:在这种情况下,您需要迭代字典中的所有键:my_dict.items.中的for word:。因此它将是另一个for循环中的for循环。请参阅我更新的答案。
for word, year_counts in my_dict.items():
    total = 0
    for year_count in year_counts:
        total += year_count.count
    print(word, total)