Python 學習 常用函式整理

2022-01-10 07:05:54 字數 4694 閱讀 1988

整理python中常用的函式

使用ast模組中的literal_eval函式來實現,把字串形式的list轉換為python的基礎型別list

from ast import

literal_eval

str_list = "

[1838, 13735, 8285, 35386]

"mylist =literal_eval(str_list)

type(mylist )

filter()函式用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。

該接收兩個引數,第乙個為函式,第二個為序列,序列的每個元素作為引數傳遞給函式進行判,然後返回 true 或 false,最後將返回 true 的元素放到新列表中。

filter(function, iterable)
python3.x 返回的結果是迭代器物件,可以使用list()函式把迭代器對轉轉換為列表物件,例如,

>>> ret = filter(lambda x: x % 2 == 0, range(10))

>>> print(list(ret))

[0, 2, 4, 6, 8]

當對list、dict進行排序時,python提供了兩個方法:

在本質上,list的排序和內建函式sorted的排序是差不多的,連引數都是一樣的,主要區別在於,list.sort()是對已經存在的列表進行操作,進而可以改變列表;而內建函式sorted返回的是乙個新的list,而不是在原來的基礎上進行的操作。返回值是乙個經過排序的可迭代型別,與iterable是一樣的。 

sorted(iterable, key=none, reverse=false)

list_obj.sort(key=none, reverse=false)

引數注釋:

key引數接收的函式形式如下,x是列表項的元素,key接受的函式必須返回值,用於表示此元素的權值,sort將按照權值的大小進行排序

def

f(x):

return len(x)

舉個例子,有如下列表,列表項是元組:

list = [('

d',3),('

a',5),('

d',1),('

c',2),('

d',2)]

1,按照元組的第二個元素對列表進行排序

>>> sorted(list,key=lambda x:x[1])[('

d', 1), ('

c', 2), ('

d', 2), ('

d', 3), ('

a', 5)]

2,對列表進行排序時,先按照元組的第一列進行排序,然後在第一列的基礎按照元組的第二列進行排序

>>> sorted(list, key = lambda x:(x[0],x[1]))[('

a', 5), ('

c', 2), ('

d', 1), ('

d', 2), ('

d', 3)]

>>> dict = ;

>>>str(dict)

""

print()函式用於列印資料

1,常規用法

常規用法的定義如下,

print(*objects, sep='

', end='

\n', file=sys.stdout)

引數注釋:

使用示例,sep、end和file引數使用預設值:

>>> name='

vic'

>>> age=22

>>> print('

my name is

',name,'

,',age, '

years old')

my name

is vic , 22 years old

2,格式化輸出格式化輸出的定義如下:

print(format % args)
引數注釋:

當args中包含多個引數時,需要使用元組形式:

>>> print('

my name is %s, %d years old

'%(name,age))

my name

is vic, 22 years old

any(iterable) :引數iterable:可迭代物件;如果當iterable有任意乙個值是非0,非空、true,那麼結果就是true。如果iterable所有的值都是0、''或false時,那麼結果為false

def any(iterable):

for element in

iterable:

ifelement:

return

true

return false

all(iterable)函式,如果當iterable有任意乙個值是0,空、false,那麼結果就是false。只有當iterable所有的值都是非0、非''或true時,結果才是true

def

any(iterable):

for element in

iterable:

ifnot

element:

return

false

return true

舉個例子,建立乙個列表[2,3,4],測試all()和 any()函式:

>>> nl=list(range(2,5))

>>> print(all(n>=2 for n in

nl))

true

>>> print(all(n>=3 for n in

nl))

false

>>> print(any(n>=3 for n in

nl))

true

map()是 python 內建的高階函式,它接收乙個函式 func 和乙個 list,並通過把函式 func 依次作用在 list 的每個元素上,得到乙個新的 list 並返回。

map(func, seq1[, seq2,…])
1,當seq只有乙個時當seq只有乙個時,把函式func作用於seq的每個元素上,得到乙個新的seq

map(lambda x: x*x , [1, 2, 3, 4, 5])

#[1, 4, 9, 10, 25]

2,當seq多於乙個時當seq多於乙個時,map可以並行地對每個seq執行func,也就是說,func有多個引數,用於接收相應序列的每個元素

>>> t=map(lambda x,y:(x**y,x+y),[1,2,3],[1,2,4])

>>> for i in

t:...

print

(i)...

(1, 2)

(4, 4)

(81, 7)

reduce()函式用於迭代計算,函式將乙個iterable中的所有資料進行下列操作:用傳給 reduce 中的函式 function(有兩個引數)先對iterable中的第 1、2 個元素進行操作,得到的結果再與iterable中的第三個元素用 function 函式運算,最後得到乙個結果。

reduce(function, iterable[, initializer])
引數注釋:

例如,initializer 是傳遞給function引數的第乙個引數,第二個引數從iterable中獲取。

>>> reduce(lambda x,y:x+y,[1,2,3,4],1)

11>>> reduce(lambda x,y:x+y,[1,2,3,4])

10

reduce()函式計算的流程如下圖所示:

zip()函式用於把可迭代的物件作為引數,把物件中對應的元素打包成乙個個元組,然後返回由這些元組組成的列表。如果各個迭代器的元素個數不一致,則返回列表長度與最短的物件相同,利用 * 號操作符,可以將元組解壓為列表。

>>>a = [1,2,3]

>>> b = [4,5,6]

>>> c = [4,5,6,7,8]

>>> zipped = zip(a,b) #

打包為元組的列表

[(1, 4), (2, 5), (3, 6)]

>>> zip(a,c) #

元素個數與最短的列表一致

[(1, 4), (2, 5), (3, 6)]

>>> zip(*zipped) #

與 zip 相反,*zipped 可理解為解壓,返回二維矩陣式

[(1, 2, 3), (4, 5, 6)]

python 排序---sort與sorted學習

python 常用函式整理

目錄 1.ceil 函式 2.類的定義 3.np.where 4.listdir 5.mean 6.split 功能 ceil 函式返回數字的上入整數 使用 imort math math.ceil x 例項 coding utf 8 import math this will import mat...

python 常用函式 整理

list.remove elem 移除第一找到的elem元素 list.pop 彈出乙個元素,返回該元素 list.pop index 1 移除列表中的乙個元素 list.extend seq 在列表末尾一次性追加另乙個序列中的多個值 用新列表擴充套件原來的列表 list.index obj 從列表...

python爬蟲常用資料整理函式

text 獲取xpath中的值。h1 text extract 0 selector的方法用於提取內容為乙個陣列。extract first 與extract 0 相同更加準確 contains 匹配乙個屬性值中包含的字串 contains class,vote post strip 把頭和尾的空格...