http://blog.sysuschool.com/u/mygod/index.html
ԺС
 
ԺС
2020-10-13 17:27:00
_֮Ӽ

ѧϰ漼ڼ䣬һνӴpythonȫֺmax()min()sorted()map()reduce()filter()zip()ǰЩȫֺʱûʲô嵽ãͦ򵥵ģϤЩ÷дܶ롣 

һ

ոսӴpythonʱӪĻ̳̣ԭΪpythonûڲ֪pythonҲpythonгlambdaʽʽ磺 

       lambda x , y : x+y  # xyx+y

       f=lambda x,n:x ** n 

       print(f(2,3))

       # IJϵͨһ

       # ĺͨӦ һʽñʽҪһֵ 

ȫֺ 

С¼ȫֺ÷

1max()min() ֵСֵ 

aԪ顢бֵȿɵֵ

       max(10, 30, 25)   # ΪԪ󣬷30

       max( [10, 30, 25] )   # беֵ30

       salaries={

         'egon':3000,

         'alex':100000000,

         'wupeiqi':10000,

         'yuanhao':2000

       }

       max(salaries)  # ֵֵyuanhaoû壬ע⴫ֵֻܷؼkey

 

2ɴkey=fnfnΪıⷽʽ

maxִʱɵûָȡɵԪֵ⣬ֵȡǼָ򽫿ɵԪֵôķֵ⣡

max()һɵ󡢵ڶkey=func

 

max

li=["10","8","20"]

print(max(li))  # 8ַȽ8

print(max(li,key=lambda x:int(x)))  # תٱȽϣ20

 

ߵmax

# ɵΪֵʱkey

salaries={

    'egon':3000,

    'alex':100000000,

    'wupeiqi':10000,

    'yuanhao':2000

}

print( max(salaries, key=lambda x:salaries[x]) )  # ôֵֵvalueֵ⣬Ӧalex

# Чú

# def get(k):

#     return salaries[k]

 

ֵɵб

# ע⴫бб

info = [

    {'name': 'egon', 'age': '18', 'salary': '3000'},

    {'name': 'wxx', 'age': '28', 'salary': '1000'},

    {'name': 'lxx', 'age': '38', 'salary': '2000'}

]

print( max(info, key=lambda dic: int(dic['salary'])))  # {'name': 'egon', 'age': '18', 'salary': '3000'} б

# ֵsalaryvalueֵЧú

# def fc(dic):

#     return int(dic['salary'])


maxͺˣصǺ⣬minͬmaxСֵġ 


2sorted()

򣬵һΪɵ󣬵ڶkey=funcһ޸ԭ󣬳Ǹֵ

ֵ򣬷ֵб

salaries={

    'egon':3000,

    'alex':100000000,

    'wupeiqi':10000,

    'yuanhao':2000

}

result =sorted(salaries)  # Ĭϰֵļ򣬷ؼб

print(result) 

# result =sorted(salaries,key=lambda x:salaries[x])  # ֵĬ򣬷keyб

result =sorted(salaries,key=lambda x:salaries[x],reverse=True)  #

print(result) 

# б򣬷صĻб

info=[

       {'name':'egon','age':'18','salary':'3000'},

       {'name':'wxx','age':'28','salary':'1000'},

       {'name':'lxx','age':'38','salary':'2000'}

]

l=sorted(info,key=lambda dic:int(dic['salary']))  # ֵб 

3map()

ѭÿԪִк浽бmap󣬿listбһΪִеĺڶΪɵԪ 

li = [11,22,33,44]

result = map(lambda x:x+100, li)   # һΪִеĺڶΪɵԪ.

print( list(result) )   # [111,122,133,144] 

names=['alex','wupeiqi','yuanhao','egon']

res=map(lambda x:x+'_NB' if x == 'egon' else x + '_SB',names)

print(list(res)) 

4reduce()

ԲԪؽۻ

import functools

li = ['wo','hao','e']?

result = functools.reduce(lambda x,y:x+y, li)

print(result)   # wohaoe 

import functools

li=[1,2,3,4,5]

re=functools.reduce(lambda x,y:x*y,li)

print(re)  # 120 

5filter()

ɸѡTrueʱѡ룬False󣬿listб

result=filter( lambda x:x > 2,[1,2,3,4])  # filter

print(list(result)) 

# ɸѡԪ

li = [11,22,33,'asd',44,'xf']

?# һ

def func(x):

    if type(x) == int:

        return True

    return False

result = filter(func,li)

print(list(result))     # [11,22,33,44]

#

result = filter(lambda x: True if type(x) == int else False ,li)

print(list(result))

?#

result = filter(lambda x: type(x) == int ,li)

print(list(result)) 

names=['alex_sb','wxx_sb','yxx_sb','egon']

res=filter(lambda x:True if x.endswith('sb') else False,names)

res=filter(lambda x:x.endswith('sb'),names)

print(list(res))        #['alex_sb', 'wxx_sb', 'yxx_sb'] 

ages=[18,19,10,23,99,30]

res=filter(lambda n:n >= 30,ages)

print(list(res))        # [99, 30] 

salaries={

    'egon':3000,

    'alex':100000000,

    'wupeiqi':10000,

    'yuanhao':2000

}

res=filter(lambda k:salaries[k] >= 10000,salaries)

print(list(res))            # ['alex', 'wupeiqi']


һzip԰ȡɵԪƴµĿɵzip[1,2],[3,4],[1,3] [2,4]

⻹ибʱõenumerateɷбб

for index, xx in enumerate(li):

print(index, xx)

ijر÷

де㳤ˣһСɡ 



mygod | Ķȫ | ظ(0) | ͨ |
ۣ
ԺС
ԺС
ʱ
ԺС
־
ԺС
ԺС
»ظ
ԺС
ҵĺ
ҵ
վϢ
ԺС
ж,ڴзŷ
Powered by Oblog.