python学习进阶一

map()函数

def format_name(s):
    return s.capitalize()

print map(format_name, ['adam', 'LISA', 'barT'])

Result:
    ['Adam', 'Lisa', 'Bart']

  

把函数作为参数

1 import math
2 
3 def add(x, y, f):
4     return f(x) + f(y)
5 
6 print add(25, 9, math.sqrt)
7 
8 Result:
9     8.0

reduce()函数

def prod(x, y):
    return x*y

print reduce(prod, [2, 4, 5, 7, 12])

Result:
    3360

  

filter()函数

import math

def is_sqr(x):
    return int(math.sqrt(x)) ** 2 == x

print([x for x in filter(is_sqr, range(1, 100))])

Result:
    [1, 4, 9, 16, 25, 36, 49, 64, 81]

  

print sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x:x.lower())

  

转载于:https://www.cnblogs.com/linkong1081/p/4862591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值