您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python3常用內置方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
max()/min()
""" 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 # 傳入了一個空的可迭代的對象參數, 返回默認值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參數接收的是一個函數對象 max函數會將lis里面的元素依次傳入轉換函數 哪個元素經過轉換函數得到的值最大, 就返回該元素 """
filter() 過濾
""" 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. """ """ 需要傳入兩個參數, 第一個是函數或者None, 第二個是可迭代對象 返回的是一個filter obj(迭代器) 如果第一個參數時None, 則返回的迭代器中只包含可迭代對象中為True的元素 如果第一參數是一個函數, 可迭代對象中元素傳入該函數, 結果為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() 映射
""" 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 """ 參數1: 函數, 參數2:可迭代對象 1.可迭代對象的中的元素依次傳入函數得到返回值res 2.調用map函數最終會得到一個迭代器對象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 """ 傳入的可迭代對象參數個數與函數所需要的參數個數是相等的 取值個數以最短的為準 """ map_obj = map(func2, [1, 2, 3], [1, 2, 3, 4]) print(list(map_obj)) # [2, 4, 6]
sorted篩選
""" 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.只傳入可迭代對象參數 res = sorted(lis) print(res) # [1, 2, 3, 4, 5] def func(x): return -x """ 2.當傳入函數時, 可迭代對象元素排序的依據是他們傳入函數得到結果 注意: 還是對原來的元素進行排序, 而不是對元素傳入函數得到的結果, 只是以這個結果為排序的依據 """ res = sorted(lis, key=func) print(res) # [5, 4, 3, 2, 1]
reduce()減少
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。