Python3常用內(nèi)置方法代碼實例
更新時間:2019年11月18日 16:43:28 作者:MrBigB
這篇文章主要介紹了Python3常用內(nèi)置方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了Python3常用內(nèi)置方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
max()/min()
- 傳入一個參數(shù) (可迭代對象), 返回這個可迭代對象中最大的元素
- 可以設(shè)置default關(guān)鍵字參數(shù), 當這個可迭代對象為空時, 返回default的值
- 傳入多個參數(shù), 返回這些參數(shù)中最大的參數(shù)
- 多個參數(shù)必須是同類型的
- 兩種方法都可以設(shè)置key關(guān)鍵字參數(shù)(傳入函數(shù))
""" max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument. """ res = max([1, 2, 3], default=0) print(res) # 3 # 傳入了一個空的可迭代的對象參數(shù), 返回默認值0 res = max([], default=0) print(res) # 0 lis = [1, 2, 3] def func(num): return -num # res = max(lis, key=func) res = max(lis, key=lambda num: -num) print(res) # 1 """ key參數(shù)接收的是一個函數(shù)對象 max函數(shù)會將lis里面的元素依次傳入轉(zhuǎn)換函數(shù) 哪個元素經(jīng)過轉(zhuǎn)換函數(shù)得到的值最大, 就返回該元素 """
filter() 過濾
- 第一個參數(shù)(形參), 要么是func, 要么是None, 不傳會報錯
- 第二個參數(shù)是可迭代對象
- 返回一個filter obj (iterator)
- filter()方法會過濾掉:
- 本身布爾值為False的元素
- 經(jīng)過函數(shù)處理后, 返回值的布爾值為False的元素
"""
filter(function or None, iterable) --> filter object
Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
"""
"""
需要傳入兩個參數(shù), 第一個是函數(shù)或者None, 第二個是可迭代對象
返回的是一個filter obj(迭代器)
如果第一個參數(shù)時None, 則返回的迭代器中只包含可迭代對象中為True的元素
如果第一參數(shù)是一個函數(shù), 可迭代對象中元素傳入該函數(shù), 結(jié)果為True, 則filter方法返回的迭代器就會包含此元素
"""
lis = [0, 1, 2, 3]
filter_obj = filter(None, lis)
print(list(filter_obj)) # [1, 2, 3]
def func(num):
if num > 1:
return 0
filter_obj = filter(func, lis)
print(list(filter_obj)) # []
filter_obj = filter(lambda num: num > 1, lis)
print(list(filter_obj)) # [2, 3]
map() 映射
- 第一個參數(shù)必須是函數(shù)
- 后面可傳入一個或多個可迭代對象參數(shù)
- 可迭代對象參數(shù)的個數(shù), 必須和函數(shù)的參數(shù)個數(shù)相同
- 多個可迭代對象包含的元素個數(shù)不一致, 則以元素個數(shù)最少的那個為準
- 返回一個map obj (iterator)
""" map(func, *iterables) --> map object Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted. """ def func1(x): return x + 1 """ 參數(shù)1: 函數(shù), 參數(shù)2:可迭代對象 1.可迭代對象的中的元素依次傳入函數(shù)得到返回值res 2.調(diào)用map函數(shù)最終會得到一個迭代器對象iterator 3. 這個iterator就包含了res """ map_obj = map(func1, [1, 2, 3]) print(list(map_obj)) # [2, 3, 4] def func2(x, y): return x + y """ 傳入的可迭代對象參數(shù)個數(shù)與函數(shù)所需要的參數(shù)個數(shù)是相等的 取值個數(shù)以最短的為準 """ map_obj = map(func2, [1, 2, 3], [1, 2, 3, 4]) print(list(map_obj)) # [2, 4, 6]
sorted篩選
- 第一個參數(shù)是可迭代對象
- 第二參數(shù)是key, 第三個參數(shù)是reverse, 這兩個參數(shù)可不傳
""" sorted(iterable, key, reverse) --> list Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order. """ lis = [3, 2, 4, 5, 1] # 1.只傳入可迭代對象參數(shù) res = sorted(lis) print(res) # [1, 2, 3, 4, 5] def func(x): return -x """ 2.當傳入函數(shù)時, 可迭代對象元素排序的依據(jù)是他們傳入函數(shù)得到結(jié)果 注意: 還是對原來的元素進行排序, 而不是對元素傳入函數(shù)得到的結(jié)果, 只是以這個結(jié)果為排序的依據(jù) """ res = sorted(lis, key=func) print(res) # [5, 4, 3, 2, 1]
reduce()減少
- 第一個參數(shù)是函數(shù)
- 該函數(shù)必須是有且只有兩個參數(shù)
- 第二個參數(shù)是序列
- initial是初始值, 可以當做序列的第一個元素
- 這個reduce指的是不斷減少的是序列中的元素個數(shù), 直到序列只剩下一個元素, 返回該元素
from functools import 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. """ lis = [1, 2, 3, 4, 5] res1 = reduce(lambda x, y: x + y, lis) print(res1) # 15 res2 = reduce(lambda x, y: x + y, lis, 1) print(res2) # 16
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Python 解決相對路徑問題:"No such file or directory"
這篇文章主要介紹了Python 解決相對路徑問題:"No such file or directory"具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python使用正則表達式實現(xiàn)爬蟲數(shù)據(jù)抽取
這篇文章主要介紹了Python使用正則表達式實現(xiàn)爬蟲數(shù)據(jù)抽取,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
使用Python編寫一個最基礎(chǔ)的代碼解釋器的要點解析
Python、Ruby等語言代碼就是在解釋器程序中一行行被解釋為機器碼同步執(zhí)行的,而如果使用Python編寫解釋器的話則可以把目標代碼解釋為Python代碼再進行解釋執(zhí)行,這里我們就來看一下使用Python編寫一個最基礎(chǔ)的代碼解釋器的要點解析:2016-07-07
Python統(tǒng)計文件中去重后uuid個數(shù)的方法
這篇文章主要介紹了Python統(tǒng)計文件中去重后uuid個數(shù)的方法,實例分析了Python正則匹配及字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-07-07

