記錄Python中常用的一些函數,備查
import os
os. getcwd()
'/Users/liudong/There is no end to learning/[05]Learning_Python/[01]Foundation/Note of Python/[04]Tricks of Python/Code'
help( os. getcwd)
Help on built-in function getcwd in module posix:
getcwd()
Return a unicode string representing the current working directory.
dir( os. getcwd)
['__call__',
'__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__name__',
'__ne__',
'__new__',
'__qualname__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__self__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__text_signature__']
## 和help返回結果相同
print( os. getcwd. __doc__)
Return a unicode string representing the current working directory.
## 統計所有元素個數
listA = [ "A", "B", "C", "A"]
len( listA)
4
## 統計某個元素的個數
listA. count( "A")
2
## 列表增加元素
x = [ 1, 2, 3]
x. extend([ 4, 3, 2, 1])
print( x)
[1, 2, 3, 4, 3, 2, 1]
## 交集
listA = [ "A", "B", "C"]
listB = [ "A", "B", "C", "D"]
list( set( listA). intersection( set( listB)))
## 或者
set( listA) & set( listB)
{'A', 'B', 'C'}
## 並集
list( set( listA). union( set( listB)))
['B', 'A', 'D', 'C']
## 差集,在B中不在A中
list( set( listB). difference( set( listA)))
['D']
## 兩種方式:是否改變原來列表
## 下面reverse的翻轉方式會改變原來list的排序
x =[ 1, 2, 3]; y =[ 1, 2, 3]
x1 = x[:: - 1]; y. reverse()
print( "now x is %s " % x);
print( "x1 is %s" % x1);
print( "y is %s" % y)
now x is [1, 2, 3]
x1 is [3, 2, 1]
y is [3, 2, 1]
## sorted不改變列表本身排序
## 可以通過賦值操作,完成排序
x =[ 3, 2, 1]
x1 = sorted( x)
## sort改變列表本身排序
## 排序後的數據直接覆蓋原列表
y =[ 3, 2, 1]
y. sort()
y1 = y
print( "sorted x is %s" % x);
print( "x1 is %s" % x1);
print( "sorted y is %s" % y);
print( "y1 is %s" % y1)
sorted x is [3, 2, 1]
x1 is [1, 2, 3]
sorted y is [1, 2, 3]
y1 is [1, 2, 3]
## 等間隔截取
x = '0123456789'
print( x[ 2: 8: 2]) # start =2 stop=10,step=2
print( x[:: 3]) ## start=beginning,stop=end,sep=3
246
0369
## 取出所有的key,或者所有value
x ={ '1': 'a', '2': 'b', '3': 'c'}
print( list( x. keys()))
print( list( x. values()))
['1', '2', '3']
['a', 'b', 'c']
## x,y同時綁定[1,2,3],改變x,y中一個列表的元素,內存地址id為91576392的值發生改變
x =[ 1, 2, 3]
y = x
y[ 0] = 3
print( "x is %s and id is %s" %( x, id( x)))
print( "y is %s and id is %s" %( y, id( y)))
x is [3, 2, 3] and id is 4571010056
y is [3, 2, 3] and id is 4571010056
## 調用函數或者模塊時,需要先添加相應的路徑
import sys
import_path = "./app/python/shandaixia/stragety"
if import_path not in sys. path:
sys. path. append( import_path)
基本用法file_object = open(file,mode = 'r')
參數含義:
使用open函數,新建文本文件,進行寫入操作。
## 進行讀寫操作
f = open( file = 'test.txt', mode = 'w') ## 該文件不存在,寫入模式自動生成文件
f. write( "line1 \nline2 \nline3") ## \n換行符號
f. close() ## 關閉文件對象
## 追加文本
f = open( file = 'test.txt', mode = 'a') ## 該文件不存在,寫入模式自動生成文件
f. write( "\nline4") ## \n換行符號
f. close() ## 關閉文件對象
文件內容讀取函數主要分為read
、readline
、readlines
。其中read
直接讀取所有字節到字符串中,readline
讀取一行數據到字符串中,readlines
讀取所有行數據到列表中,列表中每個元素代表每一行的所有字節。
f = open( file = 'test.txt', mode = 'r') ## 讀取文件
read_txt = f. read()
print( read_txt)
f. close()
line1
line2
line3
line4
## 每一次讀取都從上一步後面讀取
f = open( file = 'test.txt', mode = 'r') ## 讀取文件
readline_txt1 = f. readline()
readline_txt2 = f. readline()
readline_txt3 = f. readline()
readline_txt4 = f. readline()
print( readline_txt1)
print( readline_txt2)
print( readline_txt3)
print( readline_txt4)
f. close()
line1
line2
line3
line4
f = open( file = 'test.txt', mode = 'r') ## 讀取文件
readlines_txt = f. readlines()
print( readlines_txt)
f. close()
['line1 \n', 'line2 \n', 'line3\n', 'line4']
import glob
glob. glob( '*.ip*')
['Tricks of Python.ipynb']
import pickle
f = open( 'somedata', 'wb') #二進制打開 ,沒有該文件,新建
pickle. dump([ 1, 2, 3, 4], f) # 儲存列表
f. close() # 關閉文件
f = open( 'somedata', 'rb') # 打開文件
x = pickle. load( f) # 輸出變量
[1, 2, 3, 4]
print( 'this is True') if 1 == 1 else print( 'this is False')
this is True
判斷數據’123’是不是(str,int,list)中的某一個類型
isinstance ( '123',( str, int, list))
True