按照字典排序

按照字典排序,

按照字典的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
时间: 2024-12-27 15:03:30

按照字典排序的相关文章

字典排序及应用

字典排序: 因为我们都知道字典是无序的,所以也没有想过字典怎么排序,直到--唉 废话不多说,先看按值.按键排序的方法吧. 有一个字典dic1 = {'tired': 1, 'car': 2, 'man': 2, 'of': 1, 'front': 1, 'who': 2, 'in': 1, 'run': 2, 'exhausted': 1, 'get': 2, 'behind': 1} 按值排序:dic2 = sorted(dic1.items(),key=lambda d:d[1],rever

2016.7.30 正则表达式 字典排序

正则表达式:我要匹配字符串中两个引号中的内容 例如 str = '  map.put("abdicate","v.正式放弃(权力.责任):throne"); ' 匹配后要获得 abdicate 以及 v.正式放弃(权力.责任):throne 即引号中的红色内容 正则表达式为\"([^\"]*)\" :[^\"]是指除了"以外的字符 这样就成功得到了需要的内容.注意findall函数的使用 字典排序: key=lamb

<转>python字典排序 关于sort()、reversed()、sorted()

一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I'])) #['I', 'have', 'a', 'dream'] 2.让人糊涂的sort()与sorted() 在Python 中sorted是内建函数(BIF),而sort()是列表类型的内建函数list.sort(). sorted() sorted(iterable[,

C# 字典排序Array.Sort

Array.Sort可以实现便捷的字典排序,但如果完全相信他,那么就容易产生些异常!太顺利了,往往是前面有坑等你. 比如:微信接口,好多地方需要签名认证,签名的时候需要用的字典排序,如果只用Array.Sort()会出现签名异常的情况,而且是偶尔出现. 问题就在于他的排序默认没有区分大小写,这跟微信的签名将不匹配,所以还是需要自己实现比较的方法 public class DictionarySort : System.Collections.IComparer { public int Comp

python 字典排序 关于sort()、reversed()、sorted()

一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I'])) #['I', 'have', 'a', 'dream'] 2.让人糊涂的sort()与sorted() 在Python 中sorted是内建函数(BIF),而sort()是列表类型的内建函数list.sort(). sorted() sorted(iterable[,

华为OJ—火车进站(栈,字典排序)

给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号.其实也就是输出所有可能的出栈序列. 样例输入: 3 1 2 3 样例输出: 1 2 31 3 22 1 32 3 13 2 1 解答: 其实核心就是一个栈,对于第K个数,在第K个数进栈之前,前面的 K-1 个数要么全部出去了,要么都在栈里面,要么部分在栈里面部分出去了.那么可以假想,在第K个数入栈之前,依次从栈里面出去 0个.1个.2个--栈.si

将参数字典排序,并转为 GET 参数形式.

1.创建 NSString 分类 @interface NSString (WBRequest) - (NSString *)URLEncodedString; - (NSString *)URLEncodedStringWithCFStringEncoding:(CFStringEncoding)encoding; @end @implementation NSString (WBRequest) - (NSString *)URLEncodedString { return [self UR

全排序之字典排序

字典序全排列算法研究 一. 非递归算法(字典序法) 对给定的字符集中的字符规定了一个先后关系,在此基础上规定两个全排列的先后是从左到右逐个比较对应的字符的先后. 例如:字符集{1,2,3},较小的数字位置较先,这样按字典序生成的全排列是 123,132,213,231,312,321 ※ 一个全排列可看做一个字符串,字符串可有前缀.后缀. 生成给定全排列的下一个排列.所谓一个的下一个就是这一个与下一个之间没有其他的.这就要求这一个与下一个有尽可能长的共同前缀,也即变化限制在尽可能短的后缀上. 对

使用排序字典排序

//使用排序字典,默认只支持升序 SortedDictionary<string, string> dd = new SortedDictionary<string, string>(); dd.Add("k1","k1=v1"); dd.Add("k2","k2=v2"); //可以借助List得到降序键或值 List<String> lst = new List<String>