您好,登錄后才能下訂單哦!
今天小編給大家分享一下Python使用技巧實例分析的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
沒錯,就是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
這是一個非常有趣的庫,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
python內(nèi)置屬性,用來刪除class
類中的屬性,咱們以??途W(wǎng)隨機(jī)一道題為例
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
可以直接進(jìn)入命令提示符模式,spider和pycharm都可使用
這個庫恐怕00后全軍覆沒一首Python
詩奉上
#分享一首詩給大家,每個版本都有import this
以上就是“Python使用技巧實例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。