从字典创建对象列表

时间:2014-12-02 16:20:42

标签: python python-3.x

这需要一个字典(单词)...请注意,这是一个例子,字典的大小可能会有所不同:

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

然后我的函数以最低频率到最高频率的排序顺序返回WordCount对象列表:

def wordFrequencies(words):
    count = []   
    for item, val in words.items():
        for i in val:
            wc = createWordCount(str(item), int(i.count))
            count.append(wc)
    newcount = count
    newcount.sort(key = lambda x: x.count)
    print(newcount)

输出:

[WordCount( word='wandered', count=83769 ), WordCount( word='wandered', count=87688 ), WordCount( word='wandered', count=108634 ), WordCount( word='wandered', count=171015 ), WordCount( word='airport', count=173294 ), WordCount( word='airport', count=175702 ), WordCount( word='request', count=646179 ), WordCount( word='request', count=677820 ), WordCount( word='request', count=697645 ), WordCount( word='request', count=795265 )]

但我需要的是一个WordCount对象列表,从大多数到最不频繁的递减顺序,每次将前一个计数添加到前一个对象而不是仅添加一个新对象,它应该如下所示:

[WordCount( word=’request’, count=2816909 ),
WordCount( word=’wandered’, count=451106 ),
WordCount( word=’airport’, count=348996 )]

1 个答案:

答案 0 :(得分:1)

def wordFrequencies(words):
    count = []   
    for item, val in words.items():
        c = 0
        for i in val:
            c+=int(i.count)
        wc = createWordCount(str(item), c)
        count.append(wc)
    newcount = count
    newcount.sort(key = lambda x: x.count,reverse=True)
    print(newcount)