Python技巧 不斷更新中

2021-05-22 22:17:27 字數 857 閱讀 9555

1. 讀取檔案的內容

filepath =  "檔案路徑"

(lambda f: (f.read(), f.close()))(file(filepath))[0]  

2. 將內容寫入到檔案中

( lambda  f, d: (f.write(d), f.close()))(file(r 'd:/a.txt' ,  'w' ),  '要寫入的資料' )   

3. 根據正規表示式過濾資料夾中的檔案

import  os, re  

f1 = lambda  dir = os.getcwd() ,p =  '' : [file  for  file  in  os.listdir(dir)  if  p ==  ''

or  re.search(p, file)]  

#f2 = lambda dir = os.getcwd(), p = '': filter(lambda f: p == '' or re.search(p, f), os.listdir(dir)) 

print  f1(p = r '/.py$' )   #列出當 前目錄下所有的py檔案

#print f2(p = r'/.py$') 

4. 將b列表中的在a列表中不存在的元素新增到a列表中。

1 )  and  alist  

1) and alist

5. 序列型別的反轉。

s =  'abcd'

print  s[::- 1 ]  

l = [1 ,  2 ,  3 ,  4 ]  

print  l[::- 1 ]  

不斷更新中... ...

Python細緻技巧總結(不斷更新)

print str n 1 動態語言能用type 動態建立類,靜態語言不行 3.字串轉數字一 python中字串轉換成數字 方法1 類中進行匯入 import string str 555 num string.atoi str num即為str轉換成的數字 轉換為浮點數 string.atof s...

Python常用技巧工具 不斷更新

雙星 放在字典的前面可以讓你將字典的內容作為命名引數傳遞給函式。字典的鍵是引數的名字,鍵的值作為引數的值傳遞給函式 dictionary defsomefunction a b print a b return these do the same thing somefunction a 1,b 2...

GDB 除錯技巧(不斷更新中 )

方法 在函式前面加類名以及作用域運算子 eg break a func break 到類a的func函式 程式如下 除錯過程如下 kiosk localhost mess gdb gdb test gnu gdb gdb red hat enterprise linux 7.6 1 94.el7 l...