標籤:
python內部提供了很多內建函數。下面讓我們從a-z開始學習python的內建函數
1.1filter(function,iterable)參數iterable的元素通過函數function返回為true的所有元素構建成一個迭代器並且返回,參數iterable可以是任何可以迭代的對象,如果參數function是None,那麼刪除所有為判斷為false的元素,具體看下面執行個體#用filter求素數#首先構造一個下從3開始的奇數序列#python的產生器generator,def _odd_iter():n=1while True:n=n+2yield ndef _not_divisible(n): return lambda x:x%n>0#lambda相當於函數體為單個return語句的普通函數的匿名函數#lambda不使用return關鍵字,開發人員可以使用函數應用的位置使用lambda運算式#在開發人員想要使用一個簡單函數作為參數或者傳回值時,使用lambda運算式是很方便的#lambda用的很少,大多數使用defdef primes():yield 2 #把第一個2加進產生器it = _odd_iter() #初始化序列while True:n=next(it) #返回序列的第一個數yield n#由於primes是一個無限序列,所以調用時需要設定一個退出迴圈的條件#列印100以內的素數for n in primes():if n<100:print(n)else:break一個執行個體:l=[2,11,5,3,4,7,56,21,32,29,31,0];def func(x):if(x > 10):return Trueelse:return Falseprint(list(filter(func,l)))輸出的結果是:[11, 56, 21, 32, 29, 31]l=[2,11,5,3,4,7,56,21,32,29,31,0];def func(x):if(x > 10):return Trueelse:return Falseprint(list(filter(None,l)))輸出的結果是:[2, 11, 5, 3, 4, 7, 56, 21, 32, 29, 31]另外,需要提一個是我在別人網站看到的一個例子,自己用了很複雜的辦法做的回數是指從左向右讀和從右向左讀都是一樣的數,例如12321,909。請利用filter()濾掉非回數:def is_palindrome(n):if(str(n)[::-1] == str(n)):return Trueelse:return Falseoutput = filter(is_palindrome, range(1, 100))print(list(output))輸出的結果是:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99]沒想到這個辦法,自己用了很複雜的辦法的。Python真的很簡潔1.2float([x])強制轉換,把一個整數或者一個字串強制轉換成一個浮點數1.3format(value[,format_spec])本函數把值value按format_spec的格式來格式化,然而函數解釋format_spec是根據value的類型來決定的,不同的類型有不同的格式化解釋。當參數format_spec為空白時,本函數等同於函數str(value)的方式。其實本函數調用時,是把format(value, format_spec)的方式轉換為type(value).__format__(format_spec)方式來調用,因此在value類型裡就尋找方法__format__(),如果找不到此方法,就會返回異常TypeError。其中format_spec的編寫方式如下形式:format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]fill ::=align ::= < | > | = | ^sign ::= + | - |width ::= integerprecision ::= integertype ::= b | c | d | e | E | f | F | g | G | n | o | s | x | X | %fill是表示可以填寫任何字元。align是對齊,<是靠左對齊, >是靠右對齊,^是置中對齊。sign是符號, +表示正號, -表示負號。width是數字寬度,表示總共輸出多少位元字。precision是小數保留位元。type是輸出數字值是的表示方式,比如b是二進位表示;比如E是指數表示;比如X是十六進位表示。執行個體:print(format(3.14,"0=10"))#保留十位,其餘用0填充,在前面填充print(format(3.1515926,‘E‘))print(format(3.1415926,‘05.3‘))#保留五位,包含小數點,小數點後兩位print(format(‘hello‘,‘<20‘))#靠左對齊print(format(‘hello‘,‘^20‘))#置中對齊print(format(‘hello‘,‘>20‘))#靠右對齊print(format(3.1515926,‘+‘))#添加符號輸出的結果是:0000003.143.151593E+0003.14hello hello hello+3.15159261.4getattr(object,name[,default])返回object的name屬性的名字,如果沒有這個屬性返回false執行個體:class pep:passp = pep()print(getattr(p,‘ok‘))setattr(p,‘ok‘,‘hello‘)print(getattr(p,‘ok‘))輸出的結果是:AttributeError: ‘pep‘ object has no attribute ‘ok‘hello1.5globals()返回一個代表當前全部符號表的字典,這是當前模組(在一個函數或方法中的字典),這是它被定義的模組,而不是它被調用的模組。1.6hasattr(object, name)判斷,name是否為object的屬性,如果是返回true否則返回false。class pep:passp = pep()print(hasattr(p,‘ok‘))setattr(p,‘ok‘,‘hello‘)print(hasattr(p,‘ok‘))返回的結果是:FalseTrue1.7hash(object)返回object參數的hash值,雜湊值是整數,他們可以用來快速的進行字典查詢執行個體:print(hash(‘str‘))print(hash(1995))print(hash(0.65))輸出的結果是:926707834525168157199514987979559889011201.8hex(x)將整數integer轉換為16進位數,不能為float性,否則會報錯print(hex(2))print(hex(66))0x20x42
鐘志遠 江蘇南京 904727147
python內建函數(三)