偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

收藏,Python開發(fā)中有哪些高級技巧?

開發(fā) 后端
Python 開發(fā)中有哪些高級技巧?這是知乎上一個問題,我總結(jié)了一些常見的技巧在這里,可能談不上多高級,但掌握這些至少可以讓你的代碼看起來 Pythonic 一點。如果你還在按照類C語言的那套風(fēng)格來寫的話,在 code review 恐怕會要被吐槽了。

[[258483]]

Python 開發(fā)中有哪些高級技巧?這是知乎上一個問題,我總結(jié)了一些常見的技巧在這里,可能談不上多高級,但掌握這些至少可以讓你的代碼看起來 Pythonic 一點。如果你還在按照類C語言的那套風(fēng)格來寫的話,在 code review 恐怕會要被吐槽了。

列表推導(dǎo)式

  1. >>> chars = [ c for c in 'python' ] 
  2. >>> chars 
  3. ['p''y''t''h''o''n'

字典推導(dǎo)式

 

  1. >>> dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} 
  2. >>> double_dict1 = {k:v*2 for (k,v) in dict1.items()} 
  3. >>> double_dict1 
  4. {'a': 2, 'b': 4, 'c': 6, 'd': 8, 'e': 10} 

集合推導(dǎo)式

  1. >>> set1 = {1,2,3,4} 
  2. >>> double_set = {i*2 for i in set1} 
  3. >>> double_set 
  4. {8, 2, 4, 6} 

合并字典

 

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

復(fù)制列表

 

  1. >>> nums = [1,2,3] 
  2. >>> nums[::] 
  3. [1, 2, 3] 
  4. >>> copy_nums = nums[::] 
  5. >>> copy_nums 
  6. [1, 2, 3] 

反轉(zhuǎn)列表

 

  1. >>> reverse_nums = nums[::-1] 
  2. >>> reverse_nums 
  3. [3, 2, 1] 

PACKING / UNPACKING

變量交換

 

  1. >>> a,b = 1, 2 
  2. >>> a ,b = b,a 
  3. >>> a 
  4. >>> b 

高級拆包

 

  1. >>> a, *b = 1,2,3 
  2. >>> a 
  3. >>> b 
  4. [2, 3] 

或者

 

  1. >>> a, *b, c = 1,2,3,4,5 
  2. >>> a 
  3. >>> b 
  4. [2, 3, 4] 
  5. >>> c 

函數(shù)返回多個值(其實是自動packing成元組)然后unpacking賦值給4個變量

 

  1. >>> def f(): 
  2. ...     return 1, 2, 3, 4 
  3. ... 
  4. >>> a, b, c, d = f() 
  5. >>> a 
  6. >>> d 

列表合并成字符串

 

  1. >>> " ".join(["I""Love""Python"]) 
  2. 'I Love Python' 

鏈?zhǔn)奖容^

  1. >>> if a > 2 and a < 5: 
  2. ...     pass 
  3. ... 
  4. >>> if 2<a<5: 
  5. ...     pass 

 

yield from

 

  1. # 沒有使用 field from 
  2. def dup(n): 
  3.     for i in range(n): 
  4.         yield i 
  5.         yield i 
  6.  
  7. # 使用yield from 
  8. def dup(n): 
  9.     for i in range(n): 
  10.     yield from [i, i] 
  11.  
  12. for i in dup(3): 
  13.     print(i) 
  14.  
  15. >>> 

in 代替 or

 

  1. >>> if x == 1 or x == 2 or x == 3: 
  2. ...     pass 
  3. ... 
  4. >>> if x in (1,2,3): 
  5. ...     pass 

字典代替多個if else

 

  1. def fun(x): 
  2.     if x == 'a'
  3.         return 1 
  4.     elif x == 'b'
  5.         return 2 
  6.     else
  7.         return None 
  8.  
  9. def fun(x): 
  10.     return {"a": 1, "b": 2}.get(x) 

有下標(biāo)索引的枚舉

 

  1. >>> for i, e in enumerate(["a","b","c"]): 
  2. ...     print(i, e) 
  3. ... 
  4. 0 a 
  5. 1 b 
  6. 2 c 

生成器

注意區(qū)分列表推導(dǎo)式,生成器效率更高

  1. >>> g = (i**2 for i in range(5)) 
  2. >>> g 
  3. <generator object <genexpr> at 0x10881e518> 
  4. >>> for i in g: 
  5. ...     print(i) 
  6. ... 
  7. 16 

默認(rèn)字典 defaultdict

 

  1. >>> d = dict() 
  2. >>> d['nums'
  3. KeyError: 'nums' 
  4. >>> 
  5.  
  6. >>> from collections import defaultdict 
  7. >>> d = defaultdict(list) 
  8. >>> d["nums"
  9. [] 

字符串格式化

 

  1. >>> lang = 'python' 
  2. >>> f'{lang} is most popular language in the world' 
  3. 'python is most popular language in the world' 

列表中出現(xiàn)次數(shù)最多的元素

 

  1. >>> nums = [1,2,3,3] 
  2. >>> max(set(nums), key=nums.count
  3.  
  4. 或者 
  5. from collections import Counter 
  6. >>> Counter(nums).most_common()[0][0] 

讀寫文件

 

  1. >>> with open("test.txt""w"as f: 
  2. ...     f.writelines("hello"

判斷對象類型,可指定多個類型

 

  1. >>> isinstance(a, (int, str)) 
  2. True 

類似的還有字符串的 startswith,endswith

 

  1. >>> "http://foofish.net".startswith(('http','https')) 
  2. True 
  3. >>> "https://foofish.net".startswith(('http','https')) 
  4. True 

__str__ 與 __repr__ 區(qū)別

 

  1. >>> str(datetime.now()) 
  2. '2018-11-20 00:31:54.839605' 
  3. >>> repr(datetime.now()) 
  4. 'datetime.datetime(2018, 11, 20, 0, 32, 0, 579521)' 

前者對人友好,可讀性更強(qiáng),后者對計算機(jī)友好,支持 obj == eval(repr(obj))

使用裝飾器

 

  1. def makebold(f): 
  2. return lambda: "<b>" + f() + "</b>" 
  3.  
  4. def makeitalic(f): 
  5. return lambda: "<i>" + f() + "</i>" 
  6.  
  7. @makebold 
  8. @makeitalic 
  9. def say(): 
  10. return "Hello" 
  11.  
  12. >>> say() 
  13. <b><i>Hello</i></b> 

不使用裝飾器,可讀性非常差

 

  1. def say(): 
  2. return "Hello" 
  3.  
  4. >>> makebold(makeitalic(say))() 
  5. <b><i>Hello</i></b> 

 

責(zé)任編輯:龐桂玉 來源: Python愛好者社區(qū)
相關(guān)推薦

2016-11-25 13:34:42

Python開發(fā)

2022-11-30 08:17:41

JVM調(diào)優(yōu)技巧

2025-03-19 09:55:17

2010-04-15 10:34:16

Oracle程序開發(fā)

2011-07-27 16:11:47

開發(fā)技巧jQuery Mobi

2022-11-07 16:06:15

TypeScript開發(fā)技巧

2024-10-25 08:30:55

NumPyPandasMatplotlib

2022-09-30 10:44:47

Netty組件數(shù)據(jù)

2020-05-28 08:59:40

Python機(jī)器學(xué)習(xí)開發(fā)

2025-02-13 08:06:54

2024-05-24 08:04:12

技巧管理器數(shù)據(jù)庫

2023-05-08 15:59:17

Redis數(shù)據(jù)刪除

2024-01-15 17:26:26

JavaScriptWeb開發(fā)

2025-02-10 00:23:11

Spring微服務(wù)架構(gòu)

2023-11-27 13:53:00

Java數(shù)據(jù)轉(zhuǎn)換

2013-07-22 10:01:03

JavascriptWeb

2022-12-10 08:15:06

2020-07-10 06:10:14

Python開發(fā)代碼

2010-07-16 09:24:59

Perl模式

2024-12-04 09:27:56

點贊
收藏

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