分享10提高?Python?代碼的可讀性的技巧
1. 字符串反轉(zhuǎn)
字符串反轉(zhuǎn)有很多方法,咱們再這里介紹兩種:一種是切片,一種是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內(nèi)置函數(shù)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('設(shè)置降序', new_score2) ''' ic| '默認升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100] ic| '設(shè)置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11] '''
6.列表推導式
使用列表推導式可以快速生成一個列表或者根據(jù)列表生成滿足需求的列表
# 生成10個10-100以內(nèi)隨機整數(shù) 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
這種左右對稱的字符串。我們可以根據(jù)之前提到的切片來檢測這種特殊的字符串序列
str = '20211202' if str == str[::-1]: ? ? print('yes') else: ? ? print('no') ''' yes '''
10. 統(tǒng)計列表元素出現(xiàn)次數(shù)
統(tǒng)計列表中元素各自出現(xiàn)的次數(shù)我們使用collections
的Counter
方法
from collections import Counter lists = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd'] # 統(tǒng)計所有元素出現(xiàn)的次數(shù) counts = Counter(lists) ic(counts) # 統(tǒng)計某一元素出現(xiàn)的次數(shù) ic(counts['d']) # 統(tǒng)計出現(xiàn)最多次數(shù)的一個元素 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)] '''
到此這篇關(guān)于分享10提高 Python 代碼的可讀性的技巧的文章就介紹到這了,更多相關(guān)提高 Python 代碼可讀性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Scrapy-redis爬蟲分布式爬取的分析和實現(xiàn)
所謂的scrapy-Redis實際上就是scrapy+redis,其中對redis的操作采用redis-py客戶端。下面這篇文章詳細介紹了Scrapy-redis爬蟲分布式爬取的分析和實現(xiàn),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02python 解決mysql where in 對列表(list,,array)問題
這篇文章主要介紹了python 解決mysql where in 對列表(list,,array)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Django讀取Mysql數(shù)據(jù)并顯示在前端的實例
今天小編就為大家分享一篇Django讀取Mysql數(shù)據(jù)并顯示在前端的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05python如何將多個模型的ROC曲線繪制在一張圖(含圖例)
這篇文章主要給大家介紹了關(guān)于python如何將多個模型的ROC曲線繪制在一張圖的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02