现在的位置: 首页 > 综合 > 正文

005_004 Python 统计值 并按照次数排序 可以通过字典的值对key进行排序

2017年12月10日 ⁄ 综合 ⁄ 共 1462字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8

print '中国'

#统计值 并按照次数排序 可以通过字典的值对key进行排序

#通过字典来实现
class hist(dict):
    def add(self,item,increment=1):
        self[item]=increment + self.get(item,0)

    def counts(self,reverse=False):
        aux=[(self[k],k) for k in self]
        aux.sort(cmp=None, key=None, reverse=False)
        if reverse:
            aux.reverse()
        return [k for v,k in aux]

hist1=hist()
hist1.add(3)
hist1.add(3)
hist1.add(1)

print hist1.counts()

#通过list来实现
class histlist(list):
    def __init__(self,n):
        list.__init__(self,n*[0])
    def add(self,item,increment=1):
        self[item] += increment
    def counts(self,reverse=False):
        aux=[(v,k) for k,v in enumerate(self)]
        aux.sort(cmp=None, key=None, reverse=False)
        if reverse:aux.reverse()
        return [k for v,k in aux]
    
hist1=histlist(4)
hist1.add(3)
hist1.add(3)
hist1.add(1)

print hist1


sentence = ''' Hello there this is a test.  Hello there this was a test,
           but now it is not. '''
words = sentence.split( )
c = hist( )
for word in words: c.add(word)
print "Ascending count:"
print c.counts( )
print "Descending count:"
print c.counts(reverse=True)

from operator import itemgetter

#itemgetter为获取当前维度的值 (key,val) val为维度一 排序当前维度的值
def dict_items_sorted_by_value(d, reverse=False):
    return sorted(d.iteritems( ), key=itemgetter(1), reverse=reverse)

print dict_items_sorted_by_value(c)

打印结果如下:

中国
[1, 3]
[0, 1, 0, 2]
Ascending count:
['but', 'it', 'not.', 'now', 'test,', 'test.', 'was', 'Hello', 'a', 'is', 'there', 'this']
Descending count:
['this', 'there', 'is', 'a', 'Hello', 'was', 'test.', 'test,', 'now', 'not.', 'it', 'but']
[('not.', 1), ('it', 1), ('but', 1), ('test,', 1), ('now', 1), ('was', 1), ('test.', 1), ('a', 2), ('this', 2), ('is', 2), ('there', 2), ('Hello', 2)]

抱歉!评论已关闭.