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

提高 Python 代碼的可讀性,你需要知道的十個技巧

開發(fā) 后端
Python的可讀性和簡單性是其廣受歡迎的兩大原因,本文介紹10個常用的Python技巧來提高代碼的可讀性,并能幫助你節(jié)省大量時間,下面的技巧將在你的日常編碼練習中非常實用。

 1. 字符串反轉

[[439418]]

字符串反轉有很多方法,咱們再這里介紹兩種:一種是切片,一種是python字符串的reversed方法。 

  1. # -!- coding: utf-8 -!- 
  2. string = 'hello world' 
  3.  
  4. # 方法1 
  5. new_str = string[::-1] 
  6. ic(new_str) 
  7.  
  8. # 方法二 
  9. new_str2 = ''.join(reversed(string)) 
  10. ic(new_str2) 
  11.  
  12. ''
  13. ic| new_str: 'dlrow olleh' 
  14. ic| new_str2: 'dlrow olleh' 
  15. ''

2. 首字母大寫

這里咱們也是介紹兩種方法,區(qū)別之處在于**capitalize()**僅是首字母大寫

**title()**是每個單詞開頭的首字母都大寫 

  1. # 首字母大寫 
  2. string = 'hello python and world' 
  3.  
  4. # 方法一 
  5. new_str = string.capitalize() 
  6. ic(new_str) 
  7.  
  8.  
  9. # 方法二 
  10. new_str2 = string.title() 
  11. ic(new_str2) 
  12.  
  13. ''
  14. ic| new_str: 'Hello python and world' 
  15. ic| new_str2: 'Hello Python And World' 
  16. ''

3. 查詢唯一元素

我們利用set的唯一性來確定字符串的唯一元素: 

  1. string = 'hellohellohello' 
  2. new_str = set(string) 
  3. set類型 
  4. ic(new_str) 
  5. # 字符串類型 
  6. new_str = ''.join(new_str) 
  7. ic(new_str) 
  8.  
  9. ''
  10. ic| new_str: {'l''o''h''e'
  11. ic| new_str: 'lohe' 
  12. ''

4. 變量交換

python中的變量交換比java簡單多了,交換兩個變量無需定義第三個中間變量,直接交換即可實現(xiàn)。 

  1. a = 'hello' 
  2. b = 'world' 
  3. ic(a+b) 
  4.  
  5. # 直接交換兩個變量 
  6. a, b = b, a 
  7. ic(a+b) 
  8.  
  9. ''
  10. ic| a+b: 'helloworld' 
  11. ic| a+b: 'worldhello' 
  12. ''

5. 列表排序

列表排序這里我們也提供兩種方式。第一個是列表自帶的**sort() 方法;第二個是python內置函數 sorted()**方法。 

  1. score = [88, 99, 91, 85, 94, 85, 94, 78, 100, 80] 
  2. # 方法一 
  3. new_score = sorted(score) 
  4. ic('默認升序:', new_score) 
  5.  
  6. score = [57, 29, 11, 27, 84, 34, 87, 25, 70, 60] 
  7. # 方法二 
  8. new_score2 = sorted(score, reverse=True
  9. ic('設置降序', new_score2) 
  10.  
  11. ''
  12. ic| '默認升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100] 
  13. ic| '設置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11] 
  14. ''

6.列表推導式

使用列表推導式可以快速生成一個列表或者根據列表生成滿足需求的列表。 

  1. # 生成10個10-100以內隨機整數 
  2. numbers = [random.randint(10, 100) for x in range(10)] 
  3. ic(numbers) 
  4.  
  5. # 輸入5折后的價格 
  6. price = [800, 500, 400, 860, 780, 520, 560] 
  7. half_price = [(x*0.5)for x in price] 
  8. ic(half_price) 
  9.  
  10. ''
  11. ic| numbers: [64, 22, 80, 70, 34, 81, 74, 35, 85, 12] 
  12. ic| half_price: [400.0, 250.0, 200.0, 430.0, 390.0, 260.0, 280.0] 
  13. ''

7. 合并字符串

合并字符串我們使用string的.join()方法實現(xiàn)。 

  1. lists = ['hello''world''python''java''c++'
  2.  
  3. # 合并字符串 
  4. new_str = ' '.join(lists) 
  5. ic(new_str) 
  6.  
  7. ''
  8. ic| new_str: 'hello world python java c++' 
  9. ''

8. 拆分字符串

拆分字符串我們使用string的split()方法實現(xiàn)。 

  1. string = 'hello world python java c++' 
  2. string2 = 'hello|world|python|java|c++' 
  3.  
  4. # 拆分字符串 
  5. new_str = string.split(' '
  6. ic(new_str) 
  7.  
  8. new_str2 = string2.split('|'
  9. ic(new_str2) 
  10.  
  11. ''
  12. ic| new_str: ['hello''world''python''java''c++'
  13. ic| new_str2: ['hello''world''python''java''c++'
  14. ''

9. 回文串檢測

回文串是指aba、abba、cccbccc、aaaa這種左右對稱的字符串。我們可以根據之前提到的切片來檢測這種特殊的字符串序列。 

  1. str = '20211202' 
  2.  
  3. if str == str[::-1]: 
  4.     print('yes'
  5. else
  6.     print('no'
  7.  
  8. ''
  9. yes 
  10. ''

10. 統(tǒng)計列表元素出現(xiàn)次數

統(tǒng)計列表中元素各自出現(xiàn)的次數我們使用collections 的Counter方法。 

  1. from collections import Counter 
  2. lists = ['a''a''b''b''b''c''d''d''d''d''d'
  3.  
  4. # 統(tǒng)計所有元素出現(xiàn)的次數 
  5. counts = Counter(lists) 
  6. ic(counts) 
  7.  
  8. # 統(tǒng)計某一元素出現(xiàn)的次數 
  9. ic(counts['d']) 
  10.  
  11. # 統(tǒng)計出現(xiàn)最多次數的一個元素 
  12. ic(counts.most_common(1)) 
  13.  
  14. ''
  15. ic| counts: Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1}) 
  16. ic| counts['d']: 5 
  17. ic| counts.most_common(1): [('d', 5)] 
  18. ''

 

 

責任編輯:華軒 來源: 今日頭條
相關推薦

2022-08-29 00:37:53

Python技巧代碼

2022-08-23 14:57:43

Python技巧函數

2023-03-31 08:10:50

2019-06-06 08:48:14

代碼函數編程語言

2017-10-30 15:22:29

代碼可讀性技巧

2015-09-20 16:23:27

2024-10-11 06:00:00

Python代碼編程

2020-03-27 12:30:39

python開發(fā)代碼

2024-07-03 10:14:08

2023-06-05 16:50:06

開發(fā)TypeScriptJavaScript

2024-08-02 16:20:06

2023-10-30 18:05:55

Python類型

2024-04-03 10:29:13

JavaScrip優(yōu)化技巧

2023-01-09 17:23:14

CSS技巧

2013-03-04 09:34:48

CSSWeb

2018-09-10 09:26:33

2010-06-03 11:39:28

網絡性能

2014-07-28 10:28:25

程序員

2014-07-29 09:55:33

程序員代碼可讀性

2024-10-07 10:00:00

Python代碼編碼
點贊
收藏

51CTO技術棧公眾號