这是我的字典:
{‘请求’:YearCount( year=2005,count=646179 ),YearCount( year=2006,count=677820 ),YearCount( year=2007,count=697645 ),YearCount( year=2008,count=795265 ),‘漫游’:YearCount( year=2005,count=83769 ),YearCount( year=2006,count=83769 ),count=83769(,en29 20# ),(,),‘机场’:(,),(,)}
我需要帮助弄清楚如何访问YearCount - count值。因为我想找出每个单词的字母频率,比如“请求”、“漫游”和“机场”。我计算输入数据集中所有单词中每个字母的总数。然后,这个数字除以所有单词中的字母总数。
发布于 2014-11-27 09:34:28
如果这是您的字典,那么您可以通过以下操作访问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)https://stackoverflow.com/questions/27163419
复制相似问题