python常見內(nèi)置函數(shù)

'''
python中常見的內(nèi)置函數(shù)
'''
"""
max,找出工資最高的那個人
"""
salaries={
    'egon':3000,
    'alex':100000000,
    'wu':10000,
    'yuan':2000
}
res = max(salaries, key=lambda x: salaries[x])
print(res) # alex


"""
sorted,按字典中的value進行排序
"""
res2 = sorted(salaries, key=lambda x: salaries[x],reverse=True)
print(res2)  # ['alex', 'wu', 'egon', 'yuan']


"""
map
map(function, iterable, ...)
Map applies a function to all the items in an input_list, and return a map object,like generator
"""
l=[1,2,3]
a = map(lambda x: x**2, l)
print(list(a)) # [1, 4, 9]


"""
filter
filter(function, iterable)
filter creates a list of elements for which a function returns true
"""
salaries={
    'egon':3000,
    'alex':100000000,
    'wu':10000,
    'yuan':2000
}
#通過filter函數(shù)輸出工資大于10000的人名
c = filter(lambda m: salaries[m] > 100000,salaries)
print(list(c))   # ['alex']


"""
reduce 
reduce(function, iterable)
Reduce is a really useful function for performing some computation on a list and returning the result.
"""

from functools import reduce
l=[1, 2, 3]
b = reduce(lambda x,y: x+y,l)
print(b) # 6
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容