自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三個(gè) Python 編程技巧

開發(fā) 后端
我們知道,字典的本質(zhì)是哈希表,本身是無法排序的,但 Python 3.6 之后,字典是可以按照插入的順序進(jìn)行遍歷的,這就是有序字典,其中的原理,可以閱讀為什么 Python3.6 之后字典是有序的。

[[428472]]

今天分享 3 個(gè) Python 編程小技巧,來看看你是否用過?

1、如何按照字典的值的大小進(jìn)行排序

我們知道,字典的本質(zhì)是哈希表,本身是無法排序的,但 Python 3.6 之后,字典是可以按照插入的順序進(jìn)行遍歷的,這就是有序字典,其中的原理,可以閱讀為什么 Python3.6 之后字典是有序的。

知道了這一點(diǎn),就好辦了,先把字典的鍵值對(duì)列表排序,然后重新插入新的字典,這樣新字典就可以按照值的大小進(jìn)行遍歷輸出。代碼如下:

  1. >>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1} 
  2. >>> for k,v in xs.items():#遍歷字典 
  3. ...     print(k,v) 
  4. ... 
  5. a 4 
  6. b 3 
  7. c 2 
  8. d 1 
  9. >>> new_order = sorted(xs.items(), key=lambda x: x[1]) #對(duì)字典的鍵值對(duì)列表排序 
  10.  
  11. >>> new_xs = { k : v for k,v in new_order} #有序列表插入新的字典 
  12. >>> new_xs 
  13. {'d': 1, 'c': 2, 'b': 3, 'a': 4} 
  14. >>> for k,v in new_xs.items(): ##新字典的輸出就是有序的 
  15. ...     print(k,v) 
  16. ... 
  17. d 1 
  18. c 2 
  19. b 3 
  20. a 4 

對(duì)列表的排序,你還可以使用如下方法:

  1. >>> import operator 
  2. >>> sorted(xs.items(), key=operator.itemgetter(1)) 
  3. [('d', 1), ('c', 2), ('b', 3), ('a', 4)] 

2、優(yōu)雅的一次性判斷多個(gè)條件

假如有三個(gè)條件,只要有一個(gè)為真就可以通過,也許你會(huì)這么寫:

  1. x, y, z = 0, 1, 0 
  2.  
  3. if x == 1 or y == 1 or z == 1: 
  4.     print('passed'

實(shí)際上,以下三種方法更加 Pythonic

  1. if 1 in (x, y, z): 
  2.     print('passed'
  3.  
  4. if x or y or z: 
  5.     print('passed'
  6.  
  7. if any((x, y, z)): 
  8.     print('passed'

最后一個(gè)用到了 Python 內(nèi)置的方法 any(),any 接受一個(gè)可迭代對(duì)象作為參數(shù),比如列表或元組,只要其中一個(gè)為真,則 any() 方法返回真,用法示例如下:

  1. >>> any(['a',(2,4),3,True])  
  2. True 
  3. >>> any(['a',(2,4),3,False]) 
  4. True 
  5. >>> any(['a',(),3,False])    
  6. True 
  7. >>> any(['',(),0,False])  
  8. False 
  9. >>> any(('a',(),3,False)) 
  10. True 
  11. >>> any(('',(),0,False))  
  12. False 
  13. ## 注意空的可迭代對(duì)象返回 False 
  14. >>> any(()) 
  15. False 
  16. >>> any([]) 
  17. False 
  18. >>> any(''
  19. False 
  20. >>> any({}) 
  21. False 

與 any() 對(duì)應(yīng)的,就是方法 all(),只有全部為真,才為真,注意空的可迭代對(duì)象一直返回真。

  1. >>> all(['a',(2,4),1,True]) //list都為"真" 
  2. True 
  3. >>> all(['a',(),1,True])   //list元素中有空tuple 
  4. False 
  5. >>> all(['a',(2,4),0,True]) 
  6. False 
  7. >>> all(['a',(2,4),3,False]) 
  8. False 
  9.    
  10. ## 注意空的可迭代對(duì)象返回 True 
  11. >>>all([]) 
  12. True  
  13. >>> all(()) 
  14. True 
  15. >>> all({}) 
  16. True 
  17. >>> all(''
  18. True 

查看幫助文檔,可以在解釋器輸入 help:

  1. >>> help(all
  2. Help on built-in function all in module __builtin__: 
  3.  
  4. all(...) 
  5.     all(iterable) -> bool 
  6.  
  7.     Return True if bool(x) is True for all values x in the iterable. 
  8.     If the iterable is empty, return True

3、如何優(yōu)雅的合并兩個(gè)字典

** 操作符可以解包字典,這在合并字典時(shí)非常有用,比如:

  1. >>> x = {'a': 1, 'b': 2} 
  2. >>> y = {'b': 3, 'c': 4} 
  3.  
  4. >>> z = {**x, **y} 
  5.  
  6. >>> z 
  7. {'c': 4, 'a': 1, 'b': 3} 

如果在 Python2.x 中,需要這么做:

  1. >>> z = dict(x, **y) 
  2. >>> z 
  3. {'a': 1, 'c': 4, 'b': 3} 

本文轉(zhuǎn)載自微信公眾號(hào)「Python七號(hào)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python七號(hào)公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: Python七號(hào)
相關(guān)推薦

2022-02-21 14:14:03

SSH加密密鑰

2011-06-14 10:35:15

性能優(yōu)化

2022-01-06 22:31:21

Python技巧代碼

2022-05-02 17:52:53

Python編程語(yǔ)言

2020-07-03 07:56:34

Golang編程語(yǔ)言

2023-05-26 14:58:18

2013-01-06 11:01:59

大數(shù)據(jù)分析

2012-02-28 09:41:00

Linux管理效率技巧

2013-12-09 10:20:03

DHCP故障技巧

2010-03-10 10:41:23

Linux管理效率

2022-01-29 00:02:32

嵌入式系統(tǒng)開發(fā)系統(tǒng)

2019-04-30 09:40:41

Windows 10技巧Windows

2017-11-03 06:38:18

蓄電池保養(yǎng)環(huán)境溫度

2022-09-19 16:24:33

數(shù)據(jù)可視化Matplotlib工具

2025-02-12 10:33:44

2013-03-04 09:34:48

CSSWeb

2024-09-18 23:50:24

Python內(nèi)存生成器

2020-06-11 09:00:27

SDN網(wǎng)絡(luò)架構(gòu)網(wǎng)絡(luò)

2023-03-06 11:44:37

Postgres數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)