按照字典排序-创新互联

按照字典排序,

创新互联公司-专业网站定制、快速模板网站建设、高性价比克什克腾网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式克什克腾网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖克什克腾地区。费用合理售后完善,10多年实体公司更值得信赖。

按照字典的value排序,类似sort -k命令

import operator

x = {1:2,3:4, 4:3, 2:1, 0:0}

sorted x =sorted(x.iteritems(),

key = operator.itemgetter(1))

0表示根据key排序,1表示根据value排序

In [33]: sorted(x.iteritems(),key=operator.itemgetter(0))                                                                                                                      
Out[33]: [(0, 0), (1, 2), (3, 4), (4, 3), ('1', 2)]                                                                                                                            
                                                                                                                                                                               
In [34]: sorted(x.iteritems(),key=operator.itemgetter(1))                                                                                                                      
Out[34]: [(0, 0), (1, 2), ('1', 2), (4, 3), (3, 4)]

reverse默认为false,everse=True表示倒序排列

sorted_y = sorted(x.iteritems(),

key=operator.itemgetter(1),reverse=True)

In [35]: sorted(x.iteritems(),key=operator.itemgetter(1),reverse=True)                                                                                                         
Out[35]: [(3, 4), (4, 3), (1, 2), ('1', 2), (0, 0)]

例子:找出占用空间大的文件

os.walk  #对目录遍历

os.path.getsize #获取文件的大小

dict sort(top10)

#!/usr/bin/env python                                                                                                                                                          
#ecoding=utf8                                                                                                                                                                  
import os                                                                                                                                                                      
import sys                                                                                                                                                                     
import operator                                                                                                                                                                
                                                                                                                                                                               
def gen_dic(topdir):                                                                                                                                                           
    dic = {}                                                                                                                                                                   
    a = os.walk(topdir)                                                                                                                                                        
    for p, d, f in a:                                                                                                                                                          
        for i in f:                                                                                                                                                            
            fn = os.path.join(p,i)                                                                                                                                             
            f_size = os.path.getsize(fn)                                                                                                                                       
            dic[fn] = f_size                                                                                                                                                   
    return dic                                                                                                                                                                 
                                                                                                                                                                               
if __name__ == '__main__':                                                                                                                                                     
    dic = gen_dic(sys.argv[1]) #生成一个字典,显示argv[1]目录的文件和文件大小                                                                                                
    sorted_dic = sorted(dic.iteritems(),key=operator.itemgetter(1),reverse = True)                                                                                             
    print sorted_dic                                                                                                                                                           

[root@133 systeminformation]# pwd                                                                                                                                              
/opt/python/systeminformation                                                                                                                                                  
[root@133 systeminformation]# ll                                                                                                                                               
总用量 32                                                                                                                                                                      
-rw-r--r-- 1 root root  427 12月  2 14:58 1_md5.py                                                                                                                             
-rw-r--r-- 1 root root  453 12月  2 16:48 1_walk.py                                                                                                                            
-rw-r--r-- 1 root root  425 10月 24 05:57 hashlib2.py                                                                                                                          
drwxr-xr-x 2 root root 4096 10月 24 06:03 test                                                                                                                                 
-rw-r--r-- 1 root root  527 12月  6 16:00 top10.py                                                                                                                             
-rw-r--r-- 1 root root  430 10月 25 21:56 walk1.py                                                                                                                             
-rw-r--r-- 1 root root  128 10月 25 18:07 yield1.py                                                                                                                            
-rw-r--r-- 1 root root  680 10月 25 21:27 yield2.py                                                                                                                            
[root@133 systeminformation]# python top10.py .  #打印当前目录下文件,根据大小排序                                                                                                                               
[('./yield2.py', 680), ('./top10.py', 527), ('./1_walk.py', 453), ('./walk1.py', 430), ('./1_md5.py', 427), ('./hashlib2.py', 425), ('./yield1.py', 128), ('./test/b', 0), ('./
test/a', 0)]

[root@133 systeminformation]# vim top10.py  
#!/usr/bin/env python                                                                                                                                                          
#ecoding=utf8                                                                                                                                                                  
import os                                                                                                                                                                      
import sys                                                                                                                                                                     
import operator                                                                                                                                                                
                                                                                                                                                                               
def gen_dic(topdir):                                                                                                                                                           
    dic = {}                                                                                                                                                                   
    a = os.walk(topdir)                                                                                                                                                        
    for p, d, f in a:                                                                                                                                                          
        for i in f:                                                                                                                                                            
            fn = os.path.join(p,i)                                                                                                                                             
            f_size = os.path.getsize(fn)                                                                                                                                       
            dic[fn] = f_size                                                                                                                                                   
    return dic                                                                                                                                                                 
                                                                                                                                                                               
if __name__ == '__main__':                                                                                                                                                     
    dic = gen_dic(sys.argv[1]) #生成一个字典,显示argv[1]目录的文件和文件大小                                                                                                
    sorted_dic = sorted(dic.iteritems(),key=operator.itemgetter(1),reverse = True)                                                                                             
    for k,v in sorted_dic[:10]: #只显示0-9的大小占前10的文件                                                                                                                                               
        print k,v  
[root@133 systeminformation]# python top10.py .
./yield2.py 680
./top10.py 556
./1_walk.py 453
./walk1.py 430
./1_md5.py 427
./hashlib2.py 425
./yield1.py 128
./test/b 0
./test/a 0

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:按照字典排序-创新互联
转载来于:http://azwzsj.com/article/coegch.html