码迷,mamicode.com
首页 > 编程语言 > 详细

按照字典排序

时间:2016-12-06 23:27:18      阅读:479      评论:0      收藏:0      [点我收藏+]

标签:按照字典排序

按照字典排序,


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


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

按照字典排序

标签:按照字典排序

原文地址:http://daixuan.blog.51cto.com/5426657/1880028

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!