python3中map()和reduce()函数的使用

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

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

问题三:利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456

# -*- coding:utf-8 -*-

from functools import reduce

"""

map函数的用法:
def f(x):
    return x*x
print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])

输出结果:
[1, 4, 9, 10, 25, 36, 49, 64, 81]

            f(x) = x * x

                  │
                  │
  ┌───┬───┬───┬───┼───┬───┬───┬───┐
  │   │   │   │   │   │   │   │   │
  ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼

[ 1   2   3   4   5   6   7   8   9 ]

  │   │   │   │   │   │   │   │   │
  │   │   │   │   │   │   │   │   │
  ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼

[ 1   4   9  16  25  36  49  64  81 ]

利用map()函数,可以把一个 list 转换为另一个 list,只需要传入转换函数

--------------------------------------------------------------------

reduce() 函数会对参数序列中元素进行累积。
函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:
    用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

"""

# Q1 利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']
def normalize(L):
    return list(map(lambda name: str.title(name), L))
    
# Q1 方法二
def lower2upper(L):
    return map(lambda s: s[0:1].upper() + s[1:].lower(), L)
    
# Q2 Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积
def prod(L):
    return reduce(lambda x, y: x * y, L)
    
# Q3 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def char2num(s):
    # 字符串转换成数字
    return DIGITS[s]

def fn(x, y):
    # 将序列变换成整数
    return x*10 + y
    
def str2float(s):
    f_before = s.split('.')[0]        # 小数点前面的数字
    f_end = s.split('.')[1]            # 小数点后面的数字
    
    return reduce(fn, map(char2num, f_before)) + reduce(fn, map(char2num, f_end))/1000

# 测式
print(str2float('123.456'))

猜你喜欢

转载自www.cnblogs.com/telder/p/9166426.html