提高 Python 代碼的可讀性,你需要知道的十個技巧
1. 字符串反轉
字符串反轉有很多方法,咱們再這里介紹兩種:一種是切片,一種是python字符串的reversed方法。
- # -!- coding: utf-8 -!-
- string = 'hello world'
- # 方法1
- new_str = string[::-1]
- ic(new_str)
- # 方法二
- new_str2 = ''.join(reversed(string))
- ic(new_str2)
- '''
- ic| new_str: 'dlrow olleh'
- ic| new_str2: 'dlrow olleh'
- '''
2. 首字母大寫
這里咱們也是介紹兩種方法,區(qū)別之處在于**capitalize()**僅是首字母大寫
**title()**是每個單詞開頭的首字母都大寫
- # 首字母大寫
- string = 'hello python and world'
- # 方法一
- new_str = string.capitalize()
- ic(new_str)
- # 方法二
- new_str2 = string.title()
- ic(new_str2)
- '''
- ic| new_str: 'Hello python and world'
- ic| new_str2: 'Hello Python And World'
- '''
3. 查詢唯一元素
我們利用set的唯一性來確定字符串的唯一元素:
- string = 'hellohellohello'
- new_str = set(string)
- # set類型
- ic(new_str)
- # 字符串類型
- new_str = ''.join(new_str)
- ic(new_str)
- '''
- ic| new_str: {'l', 'o', 'h', 'e'}
- ic| new_str: 'lohe'
- '''
4. 變量交換
python中的變量交換比java簡單多了,交換兩個變量無需定義第三個中間變量,直接交換即可實現(xiàn)。
- a = 'hello'
- b = 'world'
- ic(a+b)
- # 直接交換兩個變量
- a, b = b, a
- ic(a+b)
- '''
- ic| a+b: 'helloworld'
- ic| a+b: 'worldhello'
- '''
5. 列表排序
列表排序這里我們也提供兩種方式。第一個是列表自帶的**sort() 方法;第二個是python內置函數 sorted()**方法。
- score = [88, 99, 91, 85, 94, 85, 94, 78, 100, 80]
- # 方法一
- new_score = sorted(score)
- ic('默認升序:', new_score)
- score = [57, 29, 11, 27, 84, 34, 87, 25, 70, 60]
- # 方法二
- new_score2 = sorted(score, reverse=True)
- ic('設置降序', new_score2)
- '''
- ic| '默認升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100]
- ic| '設置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11]
- '''
6.列表推導式
使用列表推導式可以快速生成一個列表或者根據列表生成滿足需求的列表。
- # 生成10個10-100以內隨機整數
- numbers = [random.randint(10, 100) for x in range(10)]
- ic(numbers)
- # 輸入5折后的價格
- price = [800, 500, 400, 860, 780, 520, 560]
- half_price = [(x*0.5)for x in price]
- ic(half_price)
- '''
- ic| numbers: [64, 22, 80, 70, 34, 81, 74, 35, 85, 12]
- ic| half_price: [400.0, 250.0, 200.0, 430.0, 390.0, 260.0, 280.0]
- '''
7. 合并字符串
合并字符串我們使用string的.join()方法實現(xiàn)。
- lists = ['hello', 'world', 'python', 'java', 'c++']
- # 合并字符串
- new_str = ' '.join(lists)
- ic(new_str)
- '''
- ic| new_str: 'hello world python java c++'
- '''
8. 拆分字符串
拆分字符串我們使用string的split()方法實現(xiàn)。
- string = 'hello world python java c++'
- string2 = 'hello|world|python|java|c++'
- # 拆分字符串
- new_str = string.split(' ')
- ic(new_str)
- new_str2 = string2.split('|')
- ic(new_str2)
- '''
- ic| new_str: ['hello', 'world', 'python', 'java', 'c++']
- ic| new_str2: ['hello', 'world', 'python', 'java', 'c++']
- '''
9. 回文串檢測
回文串是指aba、abba、cccbccc、aaaa這種左右對稱的字符串。我們可以根據之前提到的切片來檢測這種特殊的字符串序列。
- str = '20211202'
- if str == str[::-1]:
- print('yes')
- else:
- print('no')
- '''
- yes
- '''
10. 統(tǒng)計列表元素出現(xiàn)次數
統(tǒng)計列表中元素各自出現(xiàn)的次數我們使用collections 的Counter方法。
- from collections import Counter
- lists = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd']
- # 統(tǒng)計所有元素出現(xiàn)的次數
- counts = Counter(lists)
- ic(counts)
- # 統(tǒng)計某一元素出現(xiàn)的次數
- ic(counts['d'])
- # 統(tǒng)計出現(xiàn)最多次數的一個元素
- ic(counts.most_common(1))
- '''
- ic| counts: Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})
- ic| counts['d']: 5
- ic| counts.most_common(1): [('d', 5)]
- '''