python3 的一些筆記

2022-03-11 19:58:36 字數 1262 閱讀 3798

因為使用python越來越頻繁,有一些細節的東西經常用後一段時間沒去用就會忘記,做些簡單的筆記吧。

a =0

while 1:

a+=1

if(a%3==0):

print("aa"

)

else

:

print("bb"

)

#continue # 後面的全部不執行了

#pass #似乎沒影響,cc也會出來

#break #直接結束迴圈

print("cc"

)

print("dd"

) time.sleep(5)

帶有空格的class直接使用find_element_by_class_name是找不到,使用find_element_by_css_selector代替,外加獲取href的值:

browser.find_element_by_css_selector("

[class='website icon-website ng-scope']

").get_attribute('

href

')

今天進行一些爬蟲時,發現單執行緒進行還是很慢的,很多時候我們可以考慮用多執行緒。

importthreading

importlogging

# 指定時間格式

logging.basicconfig(level=logging.info,format='%(asctime)s %(message)s', datefmt='%y-%m-%d %i:%m:%s %p')

defworker():

for x in range(100):

logging.info('{} is running'.format(threading.current_thread().name))

for x in range(1, 5):

name = 'work{}'.format(x)

t = threading.thread(name=name, target=worker)

t.start()

python的多執行緒使用核心就是引入threading,然後使用它的.thread(name,target)方法,啟動多個執行緒去執行任務。

而logging的列印也差不多,可以通過basicconfig來設定列印配置。

python3 有關字典的一些用法

如 res a 1 a 2 c 3 d 4 轉為 組成字典,若重複,則是values值相加 res a 1 a 2 c 3 d 4 adict for i in range 0,len res if res i 0 in list adict.keys adict res i 0 int adict...

Python3 一些小函式的筆記

去掉列表最後一列 參考範數 np.linalg norm ytestwineqmodel.predict xtest 2 sqrt len ytest norm範數矩陣的各種範數 參考 1.n norm a,1 求a的列範數,等於a的列向量的1 範數的最大值。2.n norm a,2 求a的歐幾里德...

bpython3 推送 python3一些用法

python3自定義演算法排序 sorted a1,key functools.cmp to key cmp a1是待排序list,cmp為排序函式 numpy a np.vstack x,np.ones len x t a為x引數矩陣,vstack是新增一列1作為偏置項 a,b np.linalg...