温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

按照字典排序

发布时间:2020-07-08 13:45:12 来源:网络 阅读:402 作者:1350368559 栏目:开发技术

按照字典排序,


按照字典的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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI