day17—max, map, reduce, filter, zip 函数的使用

一、max 函数

l=[3,2,100,999,213,1111,31121,333]
print(max(l))     # 31121

dic={'k1':10,'k2':100,'k3':30}

print(max(dic))     # k3   最大的key
print(max(dic,key=lambda k:dic[k]))     # k2   最大值对应的key

print(dic[max(dic,key=lambda k:dic[k])])     # 100   最大值
print(max(dic.values()))     # 100   最大值

二、map 函数

array = [1,2,3,67,8]
print(list(map(lambda x:x-1,array)))     # [0, 1, 2, 66, 7]


#用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao']
print(list(map(lambda x:x+'_sb',name))) # ['alex_sb', 'wupeiqi_sb', 'yuanhao_sb']

res=map(lambda x:x+'_sb',name)
for i in res:
print(i) # alex_sb
# wupeiqi_sb
# yuanhao_sb
 

三、reduce 函数

from functools import reduce

array = [1,2,3,4]

print(reduce(lambda x,y:x*y,array))     # 24————1*2*3*4
print(reduce(lambda x,y:x*y,array,50))     # 1200————50*1*2*3*4,50是初始值

#**************************************************
#等同于
def reduce_test(func,array,init=None):
    l=list(array)
    if init is None:
        res=l.pop(0)
    else:
        res=init
    for i in l:
        res=func(res,i)
    return res

print(reduce_test(lambda x,y:x*y,array))
print(reduce_test(lambda x,y:x*y,array,50))


#reduce用来计算1到100的和
from functools import reduce

print(reduce(lambda x,y:x+y,range(100),100)) # 5050
print(reduce(lambda x,y:x+y,range(1,101))) # 5050

四、filter() 函数

#电影院聚集了一群看电影bb的傻逼,让我们找出他们
movie_people=['alex','wupeiqi','yuanhao','sb_alex','sb_wupeiqi','sb_yuanhao']

def tell_sb(x):
    return x.startswith('sb')

def filter_test(func,array):
    ret=[]
    for i in array:
        if func(i):
            ret.append(i)
    return ret

print(filter_test(tell_sb,movie_people))      # ['sb_alex', 'sb_wupeiqi', 'sb_yuanhao']

#或者用filter(),返回可迭代对象***************************************
print(list(filter(lambda x:x.startswith('sb'),movie_people)))      # ['sb_alex', 'sb_wupeiqi', 'sb_yuanhao']
#利用filter过滤掉千年王八,万年龟,还有一个九千岁
name_dic=[
    {'name':'alex','age':1000},
    {'name':'wupeiqi','age':10000},
    {'name':'yuanhao','age':9000},
    {'name':'linhaifeng','age':18},
]

def func(x):
    age_list=[1000,10000,9000]
    return x['age'] not in age_list

res=filter(func,name_dic)
for i in res:
    print(i)     # {'name': 'linhaifeng', 'age': 18}

print(filter(func,name_dic))     # <filter object at 0x00000000010FA748>
print(list(filter(func,name_dic)))     # [{'name': 'linhaifeng', 'age': 18}]

 五、zip 函数

#字典的运算:最小值,最大值,排序
salaries={
    'egon':3000,
    'alex':100000000,
    'wupeiqi':10000,
    'yuanhao':2000
}

#迭代字典,取得是key,因而比较的是key的最大和最小值
print(max(salaries))     # yuanhao
print(min(salaries))     # alex

#可以取values,来比较
print(max(salaries.values()))     # 100000000
print(min(salaries.values()))     # 2000

#但通常我们都是想取出,工资最高的那个人名,即比较的是salaries的值,得到的是键
print(max(salaries,key=lambda k:salaries[k]))     # alex
print(min(salaries,key=lambda k:salaries[k]))     # yuanhao

#也可以通过zip的方式实现
salaries_and_names=zip(salaries.values(),salaries.keys())

#先比较值,值相同则比较键
print(max(salaries_and_names))     # (100000000, 'alex')

#salaries_and_names是迭代器,因而只能访问一次
print(min(salaries_and_names))     #Traceback (most recent call last):
                                   #ValueError: min() arg is an empty sequence

 

posted @ 2018-10-13 21:18  *精灵鼠*  阅读(138)  评论(0编辑  收藏  举报