程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

PythonNote001---Tricks of Python

編輯:Python


記錄Python中常用的一些函數,備查

1 打印當前目錄

import
os

os. getcwd()
  • 1.
  • 2.
'/Users/liudong/There is no end to learning/[05]Learning_Python/[01]Foundation/Note of Python/[04]Tricks of Python/Code'

  • 1.

2 查看幫助

  1. help(os.getcwd) can help us learn more about the object
  2. dir(os.getcwd) 可以幫助我們獲取該對象的大部分相關屬性
  3. os.getcwd.doc為模塊、類、函數等添加說明性的文字,使程序易讀易懂,更重要的是可以通過Python自帶的標准方法將這些描述性文字信息輸出,前後各兩個下劃線
help(
os.
getcwd)

  • 1.
Help on built-in function getcwd in module posix:


getcwd()
Return a unicode string representing the current working directory.
  • 1.
  • 2.
  • 3.
  • 4.
dir(
os.
getcwd)

  • 1.
['__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__']
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
## 和help返回結果相同

print( os. getcwd. __doc__)
  • 1.
  • 2.
Return a unicode string representing the current working directory.

  • 1.

3 列表

3.1 count of list elements

## count all elements

listA = [ "A", "B", "C", "A"]
len( listA)
  • 1.
  • 2.
  • 3.
4

  • 1.
## 統計某個元素的個數

listA. count( "A")
  • 1.
  • 2.
2

  • 1.
## 列表增加元素

x = [ 1, 2, 3]
x. extend([ 4, 3, 2, 1])
print( x)
  • 1.
  • 2.
  • 3.
  • 4.
[1, 2, 3, 4, 3, 2, 1]

  • 1.

3.2 集合運算

## 交集

listA = [ "A", "B", "C"]
listB = [ "A", "B", "C", "D"]
list( set( listA). intersection( set( listB)))
## 或者
set( listA) & set( listB)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
{'A', 'B', 'C'}

  • 1.
## 並集

list( set( listA). union( set( listB)))
  • 1.
  • 2.
['B', 'A', 'D', 'C']

  • 1.
## 差集,在B中不在A中

list( set( listB). difference( set( listA)))
  • 1.
  • 2.
['D']

  • 1.

3.3 列表的逆序

## 兩種方式: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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
now x is [1, 2, 3]

x1 is [3, 2, 1]
y is [3, 2, 1]
  • 1.
  • 2.
  • 3.

列表排序

## 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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
sorted x is [3, 2, 1]

x1 is [1, 2, 3]
sorted y is [1, 2, 3]
y1 is [1, 2, 3]
  • 1.
  • 2.
  • 3.
  • 4.

4 字符串

4.1 字符串截取

## 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
  • 1.
  • 2.
  • 3.
  • 4.
246

0369
  • 1.
  • 2.

5 字典

## 取出所有的key,或者所有value

x ={ '1': 'a', '2': 'b', '3': 'c'}
print( list( x. keys()))
print( list( x. values()))
  • 1.
  • 2.
  • 3.
  • 4.
['1', '2', '3']

['a', 'b', 'c']
  • 1.
  • 2.

6 Explanation of assignment symbols

## 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)))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
x is [3, 2, 3] and id is 4571010056

y is [3, 2, 3] and id is 4571010056
  • 1.
  • 2.

7 Add module call path

## 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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

8 文件內建函數(open)

8.1 open的基本用法

基本用法​​file_object = open(file,mode = 'r')​​ 參數含義:

  1. file_nameTo open the file name string,可以是相對路徑或者是絕對路徑
  2. access_modefile open model:"r"讀取;"w"寫入,and for overwriting from the beginning;"a"結尾追加
  3. For other parameters, please refer to the help document

8.2 文件的寫入

使用open函數,新建文本文件,進行寫入操作.

## 進行讀寫操作

f = open( file = 'test.txt', mode = 'w') ## 該文件不存在,Write mode auto-generated files
f. write( "line1 \nline2 \nline3") ## \n換行符號
f. close() ## 關閉文件對象
  • 1.
  • 2.
  • 3.
  • 4.
## 追加文本

f = open( file = 'test.txt', mode = 'a') ## 該文件不存在,Write mode auto-generated files
f. write( "\nline4") ## \n換行符號
f. close() ## 關閉文件對象
  • 1.
  • 2.
  • 3.
  • 4.

8.3 文件的讀取

Read the file content mainly divided into the function​​read​​​、​​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()
  • 1.
  • 2.
  • 3.
  • 4.
line1

line2
line3
line4
  • 1.
  • 2.
  • 3.
  • 4.
## 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()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
line1


line2

line3

line4
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
f
=
open(
file
=
'test.txt',
mode
=
'r')
## 讀取文件

readlines_txt = f. readlines()
print( readlines_txt)
f. close()
  • 1.
  • 2.
  • 3.
  • 4.
['line1 \n', 'line2 \n', 'line3\n', 'line4']

  • 1.

9 Find similar files in the directory

import
glob

glob. glob( '*.ip*')
  • 1.
  • 2.
['Tricks of Python.ipynb']

  • 1.

10 permanent storage of variables

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.
  • 5.
  • 6.
[1, 2, 3, 4]

  • 1.

11 向量化的ifelse

print(
'this is True')
if
1
==
1
else
print(
'this is False')

  • 1.
this is True

  • 1.

12 isinstance

判斷數據’123’是不是(str,int,list)one of the types

isinstance (
'123',(
str,
int,
list))

  • 1.
True

  • 1.



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved