按照字典排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按照字典排序相关的知识,希望对你有一定的参考价值。

按照字典排序,


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

[[email protected] systeminformation]# pwd                                                                                                                                              
/opt/python/systeminformation                                                                                                                                                  
[[email protected] 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                                                                                                                            
[[email protected] 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)]



[[email protected] 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  
[[email protected] 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


本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1880028

以上是关于按照字典排序的主要内容,如果未能解决你的问题,请参考以下文章

python字典按照k,v来排序

python按照字典排序

按照字典的值进行排序

按照字典的值进行排序

把数字按照字典形式的进行排序

Python之按照字典的值排序