python 常用内置函数_python常用内置函数使用|python基础教程|python入门|python教程...
基礎(chǔ)小函數(shù)、字符串函數(shù)、序列函數(shù)
序列、元組、列表小函數(shù)
max() 求最大值(列表、元組、序列)
min() 求最小值
len() 求長度
>>> a = [1,2,3,4]
>>> max(a)
4
>>> min(a)
1
>>> len(a)
4
>>>
運算小函數(shù)
divmod() 求運算模,返回一個元組,第一個參數(shù)是商,第二個是余數(shù)
pow(x,y) 指數(shù)運算,x的y次方
pow(x,y,z) x的y次方,在與z取模
round() 浮點數(shù)
>>> a = 3
>>> b = 4
>>> divmod(a,b)
(0, 3)
>>> divmod(b,a)
(1, 1)
>>> pow(a,b)
81
>>> pow(a,b,8)
1
>>>
>>> a/b
0.75
>>> round(a/b)
1
>>> round(a/b,2)
0.75
>>> round(a/b,4)
0.75
>>>
其它小函數(shù)
callable() 測試函數(shù)是否可被調(diào)用
isinstance(l,list) 測試l是否是一個list
>>> def f(x):
pass
>>> callable(fc)
Traceback (most recent call last):
File "", line 1, in
callable(fc)
NameError: name 'fc' is not defined
>>> callable(f)
True
>>>
>>> l = [1,2,3,4]
>>> t = (2,3,4,5)
>>> s = 'hello'
>>> isinstance(l,list)
True
>>> isinstance(t,tuple)
True
>>> isinstance(s,str)
True
>>> isinstance(l,str)
False
字符串函數(shù)
str.capitalize() 首字母大寫
str.replace('x','y',count) 字符串替換 count 替換幾次
str.split(“”,sep) 將字符串轉(zhuǎn)換為列表,用“”切割,sep切割幾次
>>> str1 = 'hello world , today is very good day'
>>> str1.capitalize()
'Hello world , today is very good day'
>>> str1
'hello world , today is very good day'
>>> str1.replace('o','9',1)
'hell9 world , today is very good day'
>>> str1.replace('o','9',3)
'hell9 w9rld , t9day is very good day'
>>> str1.replace('o','9')
'hell9 w9rld , t9day is very g99d day'
>>>
>>> ip = '192.168.1.254'
>>> ip.split(".")
['192', '168', '1', '254']
>>> ip.split(".",1)
['192', '168.1.254']
>>>
序列函數(shù)
filter() 過濾函數(shù)
filter(f,l) 將l列表中的值傳給函數(shù)f進行判斷,保留滿足要求的數(shù)值 函數(shù)return True
zip() 將兩個列表的值進行對應(yīng),以元組存放在列表中。以最短的為組合數(shù)
map(None,a,b) 將列表a、b的值對應(yīng)起來傳給None函數(shù),None可以作為函數(shù)
fc(x,y)
reduce(fc,list) 將列表list的值依次傳輸給函數(shù)fc
>>> def f(x):
if x>5:
return True
>>> l = [1,2,3,5,6,2,3,6,7,8]
>>> filter(f,l)
>>> list(filter(f,l))
[6, 6, 7, 8]
>>>
>>> name = ['zhang','li','wang','zhou']
>>> age = [22,21,23,24]
>>> list(zip(name,age))
[('zhang', 22), ('li', 21), ('wang', 23), ('zhou', 24)]
>>> city = ['beijing','shanxi','xinjiang']
>>> list(zip(name,age,city))
[('zhang', 22, 'beijing'), ('li', 21, 'shanxi'), ('wang', 23, 'xinjiang')]
>>>
>>> def f(name,age):
return name,age
>>> list(map(f,name,age))
[('zhang', 22), ('li', 21), ('wang', 23), ('zhou', 24)]
>>>
>>> def f(x,y):
return x+y
>>> a = [1,2,3,4]
>>> b = [1,2,3,4]
>>> list(map(f,a,b))
[2, 4, 6, 8]
>>>
>>>
>>> l = range(100)
>>> reduce(f,l)
Traceback (most recent call last):
File "", line 1, in
reduce(f,l)
NameError: name 'reduce' is not defined
>>>
>>> from functools import reduce
>>> reduce(f,l)
4950
>>> l = range(101)
>>> reduce(f,l)
5050
>>>
>>>
使用reduce時需要導(dǎo)入相應(yīng)的模塊。
reduce用來計算階乘很方便。根據(jù)reduce,可以寫成一行代碼來。
>>> n = 101
>>> range(n)
range(0, 101)
>>> reduce(lambda x,y:x+y , l)
5050
>>>
+修改為*,就是求n的階乘了。不對n-1的階乘。
小例子動手寫一下,印象更深刻。
讀書和健身總有一個在路上
總結(jié)
以上是生活随笔為你收集整理的python 常用内置函数_python常用内置函数使用|python基础教程|python入门|python教程...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nfa确定化 dfa最小化_深度学习中的
- 下一篇: 两个list关联合并_算法分享---两个