日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
收藏,Python開(kāi)發(fā)中有哪些高級(jí)技巧?

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

成都創(chuàng)新互聯(lián)專注于武隆網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供武隆營(yíng)銷(xiāo)型網(wǎng)站建設(shè),武隆網(wǎng)站制作、武隆網(wǎng)頁(yè)設(shè)計(jì)、武隆網(wǎng)站官網(wǎng)定制、微信小程序定制開(kāi)發(fā)服務(wù),打造武隆網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供武隆網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

列表推導(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 

高級(jí)拆包

 
 
 
  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ù)返回多個(gè)值(其實(shí)是自動(dòng)packing成元組)然后unpacking賦值給4個(gè)變量

 
 
 
  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
  5. ...     pass 

yield from

 
 
 
  1. # 沒(méi)有使用 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 

字典代替多個(gè)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.  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] 

讀寫(xiě)文件

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

判斷對(duì)象類(lèi)型,可指定多個(gè)類(lèi)型

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

類(lèi)似的還有字符串的 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)' 

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

使用裝飾器

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

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

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

當(dāng)前名稱:收藏,Python開(kāi)發(fā)中有哪些高級(jí)技巧?
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/dhphgcj.html