Python 常用的內建函式

2022-07-11 01:45:11 字數 3205 閱讀 6159

​ build-in function,啟動python直譯器,輸入dir(__builtins__), 可以看到很多python直譯器啟動後預設載入的屬性和函式,這些函式稱之為內建函式, 這些函式因為在程式設計時使用較多,cpython直譯器用c語言實現了這些函式,啟動直譯器 時預設載入。

range

range(stop) -> list of integers

range(start, stop[, step]) -> list of integers

python2中range返回列表,python3中range返回乙個迭代值。如果想得到列表,可通過list函式

a = range(5)

list(a)

建立列表的另外一種方法

in [21]: testlist = [x+2 for x in range(5)]

in [22]: testlist

out[22]: [2, 3, 4, 5, 6]

map函式

map函式會根據提供的函式對指定序列做對映

map(...)

map(function, sequence[, sequence, ...]) -> list

#函式需要乙個引數

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

#結果為:[1, 4, 9]

#函式需要兩個引數

map(lambda x, y: x+y, [1, 2, 3], [4, 5, 6])

#結果為:[5, 7, 9]

def f1( x, y ):

return (x,y)

l1 = [ 0, 1, 2, 3, 4, 5, 6 ]

l2 = [ 'sun', 'm', 't', 'w', 't', 'f', 's' ]

l3 = map( f1, l1, l2 )

print(list(l3))

#結果為:[(0, 'sun'), (1, 'm'), (2, 't'), (3, 'w'), (4, 't'), (5, 'f'), (6, 's')]

filter函式

filter函式會對指定序列執行過濾操作

filter(...)

filter(function or none, sequence) -> list, tuple, or string

return those items of sequence for which function(item) is true. if

function is none, return the items that are true. if sequence is a tuple

or string, return the same type, else return a list.

filter函式會對序列引數sequence中的每個元素呼叫function函式,最後返回的結果包含呼叫結果為true的元素。

返回值的型別和引數sequence的型別相同

filter(lambda x: x%2, [1, 2, 3, 4])

[1, 3]

filter(none, "she")

'she'

reduce函式

reduce函式,reduce函式會對引數序列中元素進行累積

reduce(...)

reduce(function, sequence[, initial]) -> value

from left to right, so as to reduce the sequence to a single value.

for example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5). if initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.

reduce依次從sequence中取乙個元素,和上一次呼叫function的結果做引數再次呼叫function。 第一次呼叫function時,如果提供initial引數,會以sequence中的第乙個元素和initial 作為引數呼叫function,否則會以序列sequence中的前兩個元素做引數呼叫function。 注意function函式不能為none。

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

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

15reduce(lambda x, y: x+y, ['aa', 'bb', 'cc'], 'dd')

'ddaabbcc'

在python3裡,reduce函式已經被從全域性名字空間裡移除了, 它現在被放置在fucntools模組裡用的話要先引入:from functools import reduce

sorted函式
sorted(...)

sorted(iterable, cmp=none, key=none, reverse=false) --> new sorted list

in [1]: sorted([1, 4, 2, 6, 5, 3])

out[1]: [1, 2, 3, 4, 5, 6]

in [2]: sorted([1, 4, 2, 6, 5, 3],reverse = 1)

out[2]: [6, 5, 4, 3, 2, 1]

in [4]: sorted(['dd', 'aa', 'cc', 'bb'])

out[4]: ['aa', 'bb', 'cc', 'dd']

in [5]: sorted(['dd', 'aa', 'cc', 'bb'], reverse = 1)

out[5]: ['dd', 'cc', 'bb', 'aa']

python 常用的內建函式

filter function,sequence 對sequence 中的item 依次執行 function item 將執行結果為 true 的item 組成乙個 list string tuple 取決於 sequence 的型別 deff x returnx 2 0 printfilter ...

python常用的內建函式

可自定義重寫該方法,這也是python多型的體現 示例 class person def init self,name self.name name def str self return 姓名 format self.name def del self print 物件即將銷毀 當獲取不存在的屬性...

python 常用的內建函式

callable obj 檢視乙個obj是不是可以像函式一樣呼叫 repr obj 得到obj的表示字串,可以利用這個字串eval重建該物件的乙個拷貝 eval r str 表示合法的python表示式,返回這個表示式 dir obj 檢視obj的name space中可見的name hasattr...