'''
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
网友评论