python写mapreduce_怎么用Python写mapreduce,请举例说明,初学者,请赐教,不胜感激...

展开全部

1.lambda# 匿名函数

# 基本用636f70793231313335323631343130323136353331333335343934法 lambda x: x**2

# 第一个参数,然后是表达式

# 也可以使用如下

(lambda x: x**2)(5)

2. map()def map(function, sequence, *sequence_1): # real signature unknown; restored from __doc__

"""

map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of

the argument sequence(s).  If more than one sequence is given, the

function is called with an argument list consisting of the corresponding

item of each sequence, substituting None for missing values when not all

sequences have the same length.  If the function is None, return a list of

the items of the sequence (or a list of tuples if more than one sequence).

"""

return []

# 两个参数,一个处理函数,一个可迭代的序列

# 返回一个列表

# 例如 计算1到10的平方,并以列表的形式返回

map(lambda x: x**2, range(1, 11))

# 结果如下

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

# 当然 也可以如下这样使用

def square(x):

return x**2

map(square, range(1, 11))

3.reduce()def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__

"""

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,

from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5).  If initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.

"""

pass

# 两个参数,一个接受两个参数的函数,一个序列参数

# 例如 计算 1到10 的和

reduce(lambda x, y: x+y, range(1, 11))

# 当然,不适用lambda匿名函数也可以

def add(x, y):

return x+y

reduce(add, range(1, 11))

# 结果如下

45

4.filter()def filter(function_or_none, sequence): # known special case of filter

"""

filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If

function is None, return the items that are true.  If sequence is a tuple

or string, return the same type, else return a list.

"""

pass

# 接受两个参数,一个过滤函数,返回True 或者 False, 以及一个序列

# 例如, 计算100以内的偶数

filter(lambda x: x % 2 == 0, range(100))

# 如上

def div2(x):

if x % 2 == 0:

return True

else:

return False

filter(div2, range(100))

# 结果如下

[0, 2, 4, 6, 8, 10, 12, 14, 16, ... ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值