總結分享Python冷門的技巧

WBOY
發布: 2022-07-13 12:03:30
轉載
1802 人瀏覽過

這篇文章為大家帶來了關於Python的相關知識,其中主要整理了冷門的技巧的相關問題,包括了first庫、tqdm庫、delattr、!cmd操作、this庫等等內容,下面一起來看一下,希望對大家有幫助。

總結分享Python冷門的技巧

【相關推薦:Python3影片教學

first庫

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

       first is an MIT-licensed Python package with a simple function that returns the first true value from an iterable a simple function that returns the first true value from an iterable, 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 ase your use case your use#.#簡單來講就是會回傳第一個正確的可遍歷物件。

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

from first  import firstprint(first([0, None, False, 77,[], (), 42]))
登入後複製

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

import refrom first import first
re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')# re1,re2换位置结果变化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>#re2 bcwerfwer</re.match>
登入後複製

re1,re2

換位置結果變化<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">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))#&lt;re.match&gt;#re1 a&lt;/re.match&gt;</pre><div class="contentsignin">登入後複製</div></div>tqdm庫

這是一個很有趣的函式庫,

stars

不算太多,但是可以給你平淡的程式碼生活中泛起一絲漣漪。 分享一段讀取資料後並插入資料的程式碼,我想將資料插入到df2
中,只需在range前加一步即可實現視覺化,給你在枯燥的程式碼時光裡帶來一絲喜悅<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">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</pre><div class="contentsignin">登入後複製</div></div>

總結分享Python冷門的技巧
#delattr

python內建屬性,用來刪除

class

#類中的屬性,咱們以牛客網隨機一道題為例

總結分享Python冷門的技巧#       

ListNode

類別中只有一個__init__屬性,delattr函數就是人為刪去此屬性,在第一個a處會在控制台列印self.val的值,但下一個a處就會出現TypeError: ListNode() takes no arguments,這是因為屬性__init__已經被刪除,就不需要傳入x值,所以出現報錯<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">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) -&gt; ListNode:         a = ListNode(1)         delattr(ListNode, '__init__')         a = ListNode(1)# 报错b= Solution()b.reverseBetween(1,2,3)</pre><div class="contentsignin">登入後複製</div></div> !cmd操作

控制台輸入

!cmd

可以直接進入命令提示字元模式,spider和pycharm都可使用

總結分享Python冷門的技巧this庫

這個庫恐怕00後全軍覆沒一首

Python

詩奉上<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">#分享一首诗给大家,每个版本都有import this</pre><div class="contentsignin">登入後複製</div></div>

總結分享Python冷門的技巧【相關推薦:

Python3影片教學

以上是總結分享Python冷門的技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!