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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容