1. 程式人生 > >python對字典降序排序

python對字典降序排序

def test2():
    a = [['一','一','二'],['一','二','三']]
    for docno, document in enumerate(a):
        print docno,','.join(document)
    counter = defaultdict(int)
    for w in sum(a,[]):
        counter[w if isinstance(w, unicode) else unicode(w, 'utf-8')] += 1
    print counter
    print iteritems(counter)
    print sum(itervalues(counter))
    result = sorted(iteritems(counter), key=lambda d:d[1], reverse = True)
    print result