Python学习札记(二十一) 函数式编程2 map/reduce

本文深入讲解了Python中map和reduce函数的应用,包括如何使用这些函数进行数据转换和聚合操作。文章通过实例演示了如何实现字符串到整数的转换、列表元素的标准化、以及求积等操作。

参考:map/reduce

Note

1.map():map()函数接收两个参数,一个是函数,一个是Iterable。map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。

#!/usr/bin/env python3

def f(x) :
    return x*x

l = map(f, [1, 2, 3, 4, 5, 6, 7])

print(list(l))
sh-3.2# ./map1.py 
[1, 4, 9, 16, 25, 36, 49]

l是map函数返回的一个iterator,惰性序列,保存关系x*x,在调用list()函数的时候执行计算得到序列。

2.map()作为高阶函数,事实上抽象化了运算规则。

比如将一个list中的所有元素转化为字符形式:

l1 = map(str, [1, 2, 3, 4, 5, 6, 7])

print(l1)

print(list(l1))
<map object at 0x1013d89b0>
['1', '2', '3', '4', '5', '6', '7']

3.reduce()函数:"reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算",效果:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

比如一个求和序列:

def f1(x, y) :
    return x+y

l2 = reduce(f1, [1, 2, 3, 4, 5])

print(l2)

l3 = reduce(f1, 'string')

print(list(l3))
15
['s', 't', 'r', 'i', 'n', 'g']

将数字整合起来:[1, 2, 3, 4, 5] => 12345

def f2(x, y) :
    return x*10 + y

l4 = reduce(f2, [1, 2, 3, 4, 5])

print(l4)
12345

4.map(),reduce()函数相组合:

str转int(int()函数):

def func(s) :
    def func1(x) :
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[x]
    def func2(x, y) :
        return x*10 + y

    return reduce(func2, map(func1, s))

s = input()

print(func(s))
952693358
952693358

原理是先将可迭代对象str映射到由int元素组成的迭代器,即惰性序列;再将该迭代器作为参数传入reduce()函数求值。

练习:

1.利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']:

#!/usr/bin/env python3

def func(name) :
    return name.title()

l = ['adam', 'LISA', 'barT']

itor_l = map(func, l)

print(list(itor_l))
sh-3.2# ./map2.py 
['Adam', 'Lisa', 'Bart']

2.Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积:

#!/usr/bin/env python3

from functools import reduce

def prod(l) :

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

    return reduce(func, l)

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
sh-3.2# ./map3.py 
3 * 5 * 7 * 9 = 945

3.string 转 float。

#!/usr/bin/env python3

from functools import reduce

def str2float(s) :
    floatlen = 0
    s1 = ''
    flag = True
    
    for i in s :
        if i == '.' :
            flag = False
            continue
        else :
            s1 = s1+i

        if flag :
            floatlen = floatlen+1

    floatlen = len(s)-floatlen-1

    def mapfunc(s) :
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
        # return {}[]
    
    def reductfunc(x, y) :
        return x*10+y

    return reduce(reductfunc, map(mapfunc, s1)) / (10 ** floatlen)

print('str2float(\'123.456\') =', str2float('123.456'))

print('str2float(\'123.978\') =', str2float('123.978'))
sh-3.2# ./map4.py 
str2float('123.456') = 123.456
str2float('123.978') = 123.978

2017/2/11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值