python常見內置函數

'''
python中常見的內置函數
'''
"""
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函數輸出工資大于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
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容