Python高階函數(shù)、常用內置函數(shù)用法實例分析
本文實例講述了Python高階函數(shù)、常用內置函數(shù)用法。分享給大家供大家參考,具體如下:
高階函數(shù):
- 允許將函數(shù)作為參數(shù)傳入另一個函數(shù);
- 允許返回一個函數(shù)。
#返回值為函數(shù)的函數(shù) sum=lambda x,y:x+y sub=lambda x,y:x-y calc_dict={"+":sum,"-":sub} def calc(x): return calc_dict[x] print(calc('-')(5,6)) print(calc('+')(5,6)) #參數(shù)有函數(shù)的函數(shù) filter(lambda x:x>5,range(20))
常用內置函數(shù):
- abs(x):求絕對值
- range([start], stop[, step]) :產(chǎn)生一個序列,默認從0開始
- 注意:返回的不是一個list對象
>>> print(range(20)) range(0, 20) >>> type(range(20)) <class 'range'> >>> isinstance(range(20),Iterable)#########是一個可迭代對象 True >>> from collections import Iterator >>> isinstance(range(20),Iterator)#不是一個迭代器對象 False
- oct(x)
將一個數(shù)字轉化為8進制 - hex(x)
將整數(shù)x轉換為16進制字符串 - bin(x)
將整數(shù)x轉換為二進制字符串
>>> oct(8) '0o10' >>> hex(8) '0x8' >>> bin(8) '0b1000'
- chr(i):返回整數(shù)i對應的Unicode字符
- ord(x):將字符轉換成對應的Unicode編址
>>> ord('中') 20013 >>> chr(20013) '中'
- enumerate(sequence [, start = 0]):返回一個可枚舉的對象,該對象的next()方法將返回一個tuple
for i, value in enumerate(['A', 'B', 'C']): print(i, value)
- iter(o[, sentinel]) :生成一個對象的迭代器,第二個參數(shù)表示分隔符
from collections import Iterator #可以被next()函數(shù)調用并不斷返回下一個值的對象稱為迭代器:Iterator。 print(isinstance([],Iterator)) print(isinstance(iter([]),Iterator))
- sorted(iterable[, cmp[, key[, reverse]]]) 對可迭代對象進行排序
>>> l=[8,7,6,5,4,3,2,1] >>> sorted(l) [1, 2, 3, 4, 5, 6, 7, 8]
- cmp(x, y) :如果x < y ,返回負數(shù);x == y, 返回0;x > y,返回正數(shù)
- all(iterable)
1、可迭代對象中的元素都為真的時候為真
2、特別的,可迭代對象若為空返回為True
>>> l=[] >>> all(l) True >>> l=[1,2,3,4,5] >>> all(l) True >>> l=[1,2,3,4,5,0] >>> all(l) False
- any(iterable)
1、可迭代對象中的元素有一個為真的時候為真
2、特別的,可迭代對象若為空返回為False
>>> l=[] >>> any(l) False >>> l=[0,0,0,0] >>> any(l) False >>> l=[0,0,0,0,5] >>> any(l) True >>>
- eval(expression [, globals [, locals]]) :計算表達式expression的值
>>> str1="3+4" >>> eval(str1) 7
- exec(object[, globals[, locals]]):執(zhí)行儲存在字符串或文件中的 Python 語句
>>> str1="print('hello world')" >>> exec(str1) hello world
- compile(source, filename, mode[, flags[, dont_inherit]])
- 將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執(zhí)行或者eval()進行求值。
1、參數(shù)source:字符串或者AST(Abstract Syntax Trees)對象。
2、參數(shù) filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認的值。
3、參數(shù)model:指定編譯代碼的種類??梢灾付?‘exec','eval','single'。
4、參數(shù)flag和dont_inherit:這兩個參數(shù)暫不介紹
str1 = "print('hello world')" c2 = compile(str1,'','exec') exec(c2) str2="3+4" c3=compile(str2,'','eval') a=eval(c3) print(a)
- id(object) :函數(shù)用于獲取對象的內存地址
>>> id(str1) 1514678732384 >>> str2=str1 >>> id(str2) 1514678732384
- isinstance(object, classinfo):判斷object是否是class的實例
>>> isinstance(1,int) True >>> isinstance(1.0,int) False
- len(s) :返回長度(ascll格式的返回字節(jié)數(shù),unicode返回字符數(shù)/或元素個數(shù))
>>> a=b'abc' >>> len(a) 3 >>> b="我愛中國" >>> len(b) 4 >>> c=[1,2,3,4] >>> len(c) 4
- repr(object) :將對象轉化為供解釋器讀取的形式,實質是返回一個對象的 string 格式
>>> c=[1,2,3,4] >>> repr(c) '[1, 2, 3, 4]' >>> d={1:2,2:3,3:4} >>> repr(d) '{1: 2, 2: 3, 3: 4}'
- type(object) :返回該object的類型
>>> type(1) <class 'int'> >>> type("123") <class 'str'> >>> type((1,2,3)) <class 'tuple'>
關于Python相關內容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數(shù)據(jù)結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
從DataFrame中提取出Series或DataFrame對象的方法
今天小編就為大家分享一篇從DataFrame中提取出Series或DataFrame對象的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11PyTorch中torch.nn.functional.cosine_similarity使用詳解
在pytorch中可以使用torch.cosine_similarity函數(shù)對兩個向量或者張量計算余弦相似度,這篇文章主要給大家介紹了關于PyTorch中torch.nn.functional.cosine_similarity使用的相關資料,需要的朋友可以參考下2022-03-03使用fdopen實現(xiàn)對Python進程產(chǎn)生的文件進行權限最小化配置
用python進行文件的創(chuàng)建和讀寫操作時,我們很少關注所創(chuàng)建的文件的權限配置。本文就來聊聊如何使用fdopen實現(xiàn)對Python進程產(chǎn)生的文件進行權限最小化配置吧2023-03-03python中django框架通過正則搜索頁面上email地址的方法
這篇文章主要介紹了python中django框架通過正則搜索頁面上email地址的方法,涉及django框架及正則表達式的使用技巧,需要的朋友可以參考下2015-03-03Python?Prometheus接口揭秘數(shù)據(jù)科學新技巧
本篇文章將分享Prometheus?API的基本概念到PromQL查詢語言的應用,再到如何通過Python與Prometheus?API進行無縫交互,通過豐富的示例代碼和詳細的講解,將解鎖使用Python進行實時監(jiān)控的奇妙世界,為讀者打開更廣闊的數(shù)據(jù)分析視野2024-01-01