码迷,mamicode.com
首页 > 编程语言 > 详细

python mapreduce习题

时间:2015-11-14 23:24:08      阅读:449      评论:0      收藏:0      [点我收藏+]

标签:

from functools import reduce


def str2int(s):
    def char2int(c):
        return {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4,
                "5": 5, "6": 6, "7": 7, "8": 8, "9": 9}[c]

    return reduce(lambda x, y: x * 10 + y, map(char2int, s))


print(str2int("98986553"))

 上题,将字符串变为int

1.

# 利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。
# 输入:[‘adam‘, ‘LISA‘, ‘barT‘],输出:[‘Adam‘, ‘Lisa‘, ‘Bart‘]:
m = map(lambda s: s[:1].upper() + s[1:].lower(), [adam, LISA, barT])
print(list(m))

 

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

 

# 利用map和reduce编写一个str2float函数,把字符串‘123.456‘转换成浮点数123.456:
def str2float(s):
    return reduce(lambda x, y: x + y * (0.1 ** (len(str(y)))), map(int, s.split(.)))

 

python mapreduce习题

标签:

原文地址:http://www.cnblogs.com/fangjianbin/p/4965802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!