Created
          May 16, 2017 16:11 
        
      - 
      
- 
        Save Everfighting/58dacb9001ad86bd5cb6b3df63bcf4ec to your computer and use it in GitHub Desktop. 
    根据字典的值大小对字典的项排序
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| {x: ranin(60,100) for x in 'xyzabc'} | |
| # {'a':97,'b':69,'c':78,'x':66,'y':95,'z':72} | |
| sorted(d) | |
| # ['a','b','c','x','y','z'] | |
| iter(d) | |
| # dictionary-keyiterator at 0x7f030d0d1f18 | |
| list(iter(d)) | |
| ['a','c','b','y','x','z'] | |
| (97,'a') > (69,'b') | |
| True | |
| (97,'a') > (97,'b') | |
| False | |
| d.keys() | |
| ['a','c','b','y','x','z'] | |
| d.values() | |
| [85,94,88,96,85,84] | |
| zip(d.values(),d.keys()) | |
| [(85,'a'),(94,'c'),(88,'b'),(96,'y'),(85,'x'),(84,'z')] | |
| # 下面这个省空间 | |
| zip(d.itervalues(),d.iterkeys()) | |
| [(85,'a'),(94,'c'),(88,'b'),(96,'y'),(85,'x'),(84,'z')] | |
| sorted(zip(d.itervalues(),d.iterkeys())) | |
| [(84,'z'),(85,'a'),(85,'x'),(88,'b'),(94,'c'),(96,'y')] | |
| # 方法二 | |
| d.items() | |
| [('a',85),('c',4),('b',88,('y',96),('x',85),('z',84)] | |
| sorted(d.items(),key = lambda x:x[1]) | |
| [('z',84),('a',85),('x',85),('b',88),('c',94),('y',96)] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment