成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

 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. 首字母大寫

這里咱們也是介紹兩種方法,區別之處在于**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簡單多了,交換兩個變量無需定義第三個中間變量,直接交換即可實現。 

  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()方法實現。 

  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()方法實現。 

  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. 統計列表元素出現次數

統計列表中元素各自出現的次數我們使用collections 的Counter方法。 

  1. from collections import Counter 
  2. lists = ['a''a''b''b''b''c''d''d''d''d''d'
  3.  
  4. # 統計所有元素出現的次數 
  5. counts = Counter(lists) 
  6. ic(counts) 
  7.  
  8. # 統計某一元素出現的次數 
  9. ic(counts['d']) 
  10.  
  11. # 統計出現最多次數的一個元素 
  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-23 14:57:43

Python技巧函數

2022-08-29 00:37:53

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

2020-03-27 12:30:39

python開發代碼

2024-10-11 06:00:00

Python代碼編程

2024-07-03 10:14:08

2023-06-05 16:50:06

開發TypeScriptJavaScript

2024-08-02 16:20:06

2024-04-03 10:29:13

JavaScrip優化技巧

2013-03-04 09:34:48

CSSWeb

2023-01-09 17:23:14

CSS技巧

2018-09-10 09:26:33

2023-10-30 18:05:55

Python類型

2010-06-03 11:39:28

網絡性能

2025-05-12 10:00:00

JavaScript代碼編碼

2014-07-28 10:28:25

程序員

2014-07-29 09:55:33

程序員代碼可讀性
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本视频一区二区 | 天天天插 | 精品国产欧美 | 亚洲在线一区二区三区 | 亚洲成人免费网址 | 亚洲性在线| 国产乱码精品一区二区三区中文 | 久久精品视频在线观看 | 国产亚洲一区二区精品 | 国产九九精品 | 国产一级大片 | 久久精品视频网站 | 欧美一区二区三区 | 久久精品国产99国产精品 | 99亚洲精品| 久久免费精品视频 | av天天爽| 99re在线 | 欧美精品一区二区免费 | 亚洲欧美在线观看 | 国产免费看| 亚洲人在线 | 狠狠涩| 国产成人精品综合 | 国产免费一区二区 | 日韩中文字幕视频 | 日韩在线免费视频 | 中文天堂在线一区 | 国产欧美视频一区二区三区 | 成人在线精品视频 | 欧美一区| 99精品在线免费观看 | 我要看黄色录像一级片 | 国产成人av在线播放 | 国产精品一区二区在线播放 | 久久青| 亚洲 成人 av| 久久久久久免费精品一区二区三区 | 久久久综合色 | 黄色精品 | 亚洲网站在线观看 |