這篇文章主要為大家展示了“Python中內(nèi)置方法有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Python中內(nèi)置方法有哪些”這篇文章吧。

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)嘉峪關(guān)免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
abs() 取絕對值
dict() 數(shù)據(jù)轉(zhuǎn)成字典
min() 從列表取最小值
>>> a = [1,4,5,-1,3] >>> min(a) -1 >>> max(a) 5 >>>
all () 如果集合內(nèi)所有數(shù)據(jù)都是True ,則返回True,否則返回 FALSE(0是false,其它都是True),情況而如果集合是空,返回true。
all(iterable, /) Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True. >>> a = [1,4,5,-1,3] >>> all(a) True >>> a.append(0) >>> a [1, 4, 5, -1, 3, 0] >>> all(a) False >>> a = [] >>> all(a) True >>> bool(a) #測試布爾判斷則為FALSE False >>>
any() 只要有一個值為True 結(jié)果即為True,如果集合為空位False。
any(iterable, /) Return True if bool(x) is True for any x in the iterable. If the iterable is empty, return False. >>> a = [False,0] >>> any(a) False >>> a = [False,0,1] >>> any(a) True >>>
dir()打印當(dāng)前所有變量
hex() 數(shù)字轉(zhuǎn)16進制
slice() 切片
divmod() 分別取除的整數(shù)和余數(shù)
>>> 10%3 1 >>> 10//3 3 >>> divmod(10,3) (3, 1) >>>
id() 求數(shù)據(jù)內(nèi)存地址
item() 字典變列表
sorted() 排序
>>> l [0, 1, 2, 3, 55, 5, 6, 7, 8, 9] >>> sorted(l) [0, 1, 2, 3, 5, 6, 7, 8, 9, 55]
enumerate() 枚舉
oct()轉(zhuǎn)8進制
bin()轉(zhuǎn)2jinzhi
eval()按解釋器規(guī)則 把字符串轉(zhuǎn)代碼 只能轉(zhuǎn)單行
>>> f = '1+3/2'
>>> f
'1+3/2'
>>> eval(f)
2.5
>>>
>>> eval('print("hello the world")')
hello the world
>>>exec() 功能與eval 一樣 ,區(qū)別在于 能多行
>>> code = '''
... if 3 > 5 :
... print('aaaa')
... else :
... print('bbbb')
... '''
>>> exec(code)
bbbb
>>>
#exec與 eval另一區(qū)別
>>> res = eval('1+2+3')
>>> res2 = exec('4+5+6')
>>> print(res,res2)
6 None #exec無法拿到返回的值
>>>ord() 查詢ascill碼位置
chr() ASCII碼位置返回具體值
>>> ord('a')
97
>>> chr(97)
'a'sum() 集合求和
>>> l [0, 1, 2, 3, 55, 5, 6, 7, 8, 9] >>> sum(l) 96 >>>
bytearray()
map()
Python中的map函數(shù)應(yīng)用于每一個可迭代的項,返回的是一個結(jié)果list。如果有其他的可迭代參數(shù)傳進來,map函數(shù)則會把每一個參數(shù)都以相應(yīng)的處理函數(shù)進行迭代處理。map()函數(shù)接收兩個參數(shù),一個是函數(shù),一個是序列,map將傳入的函數(shù)依次作用到序列的每個元素,并把結(jié)果作為新的list返回。
有一個list, L = [1,2,3,4,5,6,7,8],我們要將f(x)=x^2作用于這個list上,那么我們可以使用map函數(shù)處理。
>>> L = [1,2,3,4,] >>> def pow2(x): ... return x*x ... >>> map(pow2,L) [1, 4, 9, 16] #eg2 >>> list(map(lambda x : x*x ,[1,2,3,4,5])) [1, 4, 9, 16, 25]
filter()
>>> list(filter(lambda x:x>3,[1,2,3,4,5])) [4, 5]
redue
import functools #phthon2功能 3需要導(dǎo)入 >>> functools.reduce() >>> functools.reduce(lambda x,y:x+y,[1,2,3,4,22,3]) 35 >>> functools.reduce(lambda x,y:x+y,[1,2,3,4,22,3],50) 85 >>> reduce(...) reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, 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.
以上是“Python中內(nèi)置方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:Python中內(nèi)置方法有哪些
網(wǎng)站鏈接:http://www.chinadenli.net/article8/gccoip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、電子商務(wù)、搜索引擎優(yōu)化、定制開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)