溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python使用技巧實例分析

發(fā)布時間:2022-07-13 13:37:19 來源:億速云 閱讀:160 作者:iii 欄目:編程語言

今天小編給大家分享一下Python使用技巧實例分析的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

Python使用技巧實例分析

first庫

沒錯,就是first,這是個庫的名稱,目前124個stars

first is an MIT-licensed Python package with a simple function that returns the first true value from an iterable, or None if there is none. If you need more power, you can also supply a key function that is used to judge the truth value of the element or a default value if None doesn’t fit your use case.

簡單來講就是會返回第一個正確的可遍歷對象。

如第一個例子,第一個正確的可遍歷對象為`77`

from first  import firstprint(first([0, None, False, 77,[], (), 42]))

第二個例子用了re正則,我在其基礎(chǔ)上進(jìn)行改動,以便大家更容易理解。

import refrom first import first
re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')# re1,re2換位置結(jié)果變化m = first(regexp.match('abcwerfwer') for regexp in [ re2,re1])print(m)if not m:
   print('no match!')elif m.re is re1:
   print('re1', m.group(1))elif m.re is re2:
   print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re2 bcwerfwer

re1,re2換位置結(jié)果變化

import refrom first import first
re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')m = first(regexp.match('abcwerfwer') for regexp in [re1, re2])print(m)if not m:
   print('no match!')elif m.re is re1:
   print('re1', m.group(1))elif m.re is re2:
   print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re1 a

tqdm庫

這是一個非常有趣的庫,stars不算太多,但是可以給你平淡的代碼生活中泛起一絲漣漪。
分享一段讀取數(shù)據(jù)后并插入數(shù)據(jù)的代碼,我想將數(shù)據(jù)插入到df2中,只需在range前加一步即可實現(xiàn)可視化,給你在枯燥的代碼時光里帶來一絲喜悅

from tqdm import tqdm# 還可以用以下辦法是一個道理# from tqdm import trange# for i in trange(0,len(year),96):print(len(year))for i in tqdm(range(0,len(year),96)):
        # print(temp[i:i+96],len(temp[i:i+96]))
        try:
                df2.loc[index,3:99] = list(np.insert(df2.values[:,3:99], index, values=temp[i:i+96], axis=0)[index])
                # print(temp[i:i+96])
                # df.insert(1, '0:00', value=temp[i:i+96], allow_duplicates=True)
                # print(index,'+',len(year))
        except Exception as e:
                pass
        index+=1

delattr

python內(nèi)置屬性,用來刪除class類中的屬性,咱們以??途W(wǎng)隨機(jī)一道題為例

Python使用技巧實例分析

ListNode類中只有一個__init__屬性,delattr函數(shù)就是人為刪去此屬性,在第一個a處會在控制臺打印self.val的值,但下一個a處就會出現(xiàn)TypeError: ListNode() takes no arguments,這是因為屬性__init__已經(jīng)被刪除,就不需要傳入x值,所以出現(xiàn)報錯

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
        print(self.val)class Solution:
    def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        a = ListNode(1)
        delattr(ListNode, '__init__')
        a = ListNode(1)# 報錯b= Solution()b.reverseBetween(1,2,3)

!cmd操作

控制臺輸入!cmd可以直接進(jìn)入命令提示符模式,spider和pycharm都可使用

Python使用技巧實例分析

this庫

這個庫恐怕00后全軍覆沒一首Python詩奉上

#分享一首詩給大家,每個版本都有import this

Python使用技巧實例分析

以上就是“Python使用技巧實例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI