記錄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.
## count all elements
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']
## 兩種方式:whether to change the original list
## 下面reverseThe flipping method will change the originallist的排序
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]
## sortedDoes not change the ordering of the list itself
## By assigning,完成排序
x =[ 3, 2, 1]
x1 = sorted( x)
## sortChange the sorting of the list itself
## The sorted data directly overwrites the original list
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]
## Cut at equal intervals
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,ythe elements of a list in,內存地址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
## When calling a function or module,You need to add the corresponding path first
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') ## 該文件不存在,Write mode auto-generated files
f. write( "line1 \nline2 \nline3") ## \n換行符號
f. close() ## 關閉文件對象
## 追加文本
f = open( file = 'test.txt', mode = 'a') ## 該文件不存在,Write mode auto-generated files
f. write( "\nline4") ## \n換行符號
f. close() ## 關閉文件對象
Read the file content mainly divided into the functionread
、readline
、readlines
.其中read
read all bytes directly into a string,readline
Read a line of data in the string,readlines
Read all row data into a list,Each element in the list represents all the bytes of each row.
f = open( file = 'test.txt', mode = 'r') ## 讀取文件
read_txt = f. read()
print( read_txt)
f. close()
line1
line2
line3
line4
## Each read reads from the end of the previous step
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)one of the types
isinstance ( '123',( str, int, list))
True