1. 程式人生 > 程式設計 >Python高階函式、常用內建函式用法例項分析

Python高階函式、常用內建函式用法例項分析

本文例項講述了Python高階函式、常用內建函式用法。分享給大家供大家參考,具體如下:

高階函式:

  • 允許將函式作為引數傳入另一個函式;
  • 允許返回一個函式。
#返回值為函式的函式
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))

#引數有函式的函式
filter(lambda x:x>5,range(20))

常用內建函式:

  • abs(x):求絕對值
  • range([start],stop[,step]) :產生一個序列,預設從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)
    將一個數字轉化為8進位制
  • hex(x)
    將整數x轉換為16進位制字串
  • bin(x)
    將整數x轉換為二進位制字串
>>> oct(8)
'0o10'
>>> hex(8)
'0x8'
>>> bin(8)
'0b1000'
  • chr(i):返回整數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]) :生成一個物件的迭代器,第二個引數表示分隔符
from collections import Iterator
#可以被next()函式呼叫並不斷返回下一個值的物件稱為迭代器: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,8]
  • cmp(x,y) :如果x < y,返回負數;x == y,返回0;x > y,返回正數
  • all(iterable)
    1、可迭代物件中的元素都為真的時候為真
    2、特別的,可迭代物件若為空返回為True
>>> l=[]
>>> all(l)
True
>>> l=[1,5]
>>> all(l)
True
>>> l=[1,0]
>>> all(l)
False
  • any(iterable)
    1、可迭代物件中的元素有一個為真的時候為真
    2、特別的,可迭代物件若為空返回為False
>>> l=[]
>>> any(l)
False
>>> l=[0,0]
>>> any(l)
False
>>> l=[0,5]
>>> any(l)
True
>>>
  • eval(expression [,globals [,locals]]) :計算表示式expression的值
>>> str1="3+4"
>>> eval(str1)
7
  • exec(object[,globals[,locals]]):執行儲存在字串或檔案中的 Python 語句
>>> str1="print('hello world')"
>>> exec(str1)
hello world
  • compile(source,filename,mode[,flags[,dont_inherit]])
    • 將source編譯為程式碼或者AST物件。程式碼物件能夠通過exec語句來執行或者eval()進行求值。
      1、引數source:字串或者AST(Abstract Syntax Trees)物件。
      2、引數 filename:程式碼檔名稱,如果不是從檔案讀取程式碼則傳遞一些可辨認的值。
      3、引數model:指定編譯程式碼的種類。可以指定為 ‘exec','eval','single'。
      4、引數flag和dont_inherit:這兩個引數暫不介紹
str1 = "print('hello world')"
c2 = compile(str1,'','exec')  
exec(c2)

str2="3+4"
c3=compile(str2,'eval')
a=eval(c3)
print(a)
  • id(object) :函式用於獲取物件的記憶體地址
>>> 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格式的返回位元組數,unicode返回字元數/或元素個數)
>>> a=b'abc'
>>> len(a)
3
>>> b="我愛中國"
>>> len(b)
4
>>> c=[1,4]
>>> len(c)
4
  • repr(object) :將物件轉化為供直譯器讀取的形式,實質是返回一個物件的 string 格式
>>> c=[1,4]
>>> repr(c)
'[1,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,3))
<class 'tuple'>

關於Python相關內容感興趣的讀者可檢視本站專題:《Python函式使用技巧總結》、《Python面向物件程式設計入門與進階教程》、《Python資料結構與演算法教程》、《Python字串操作技巧彙總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》

希望本文所述對大家Python程式設計有所幫助。