首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义accumulate_n?(高阶函数)

定义accumulate_n?(高阶函数)
EN

Stack Overflow用户
提问于 2017-03-19 05:32:17
回答 2查看 1.1K关注 0票数 2

我想使用accumulate_n(op, init, sequences)定义一个函数accumulate(op, init, seq)

函数accumulate_n(op, init, sequences)类似于accumulate(op, init, seq),只不过它将等长序列作为其第三个参数。它应用累加函数op将序列的所有第一元素、序列的所有第二元素等组合起来,并返回结果序列。例如,如果s是一个包含四个序列的序列,[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],那么accumulate_n(lambda x, y: x+y, 0, s)的值应该是序列[22, 26, 30]

代码语言:javascript
复制
def accumulate(op, init, seq):
    if not seq:
        return init
    else:
        return op(seq[0], accumulate(op, init, seq[1:]))

def accumulate_n(op, init, sequences):
    if (not sequences) or (not sequences[0]):
        return type(sequences)()
    else:
        return ( [accumulate(op, init, ??)]
               + accumulate_n(op, init, ??) )

但是我被困在了??部分,因为我不知道应该在其中包含什么。我想我可以使用map内置函数,但仍然不确定要做什么。

下面是函数应该执行的示例:

代码语言:javascript
复制
accumulate_n(lambda x,y: x+y, 0, [[1,2],[3,4],[5,6]])   
# [9, 12]
accumulate_n(lambda x,y: x+y, 0, [[1,4],[5,7],[9,10]])  
# [15, 21]
accumulate_n(lambda x,y: x+y, 0, [[9,8],[7,6],[5,4]])   
# [21, 18]

如果有人能帮忙,我会很感激的!谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-19 07:42:56

看上去你是想要:

  • 每个序列的第一个元素的列表,可以是[s[0] for s in sequences],和
  • 每个序列的剩余元素的列表,可以是[s[1:] for s in sequences]

总之:

代码语言:javascript
复制
def accumulate(op, init, seq):
    if not seq:
        return init
    else:
        return op(seq[0], accumulate(op, init, seq[1:]))

def accumulate_n(op, init, sequences):
    if (not sequences) or (not sequences[0]):
        return type(sequences)()
    else:
        heads = [s[0] for s in sequences]
        tails = [s[1:] for s in sequences]

        return ([accumulate(op, init, heads)]
               + accumulate_n(op, init, tails))

但是,不管它的价值是什么,我会按行进行,而不是按列进行,并要求init本身是序列:

代码语言:javascript
复制
def accumulate_n_(op, init, sequences):
    try:
        head = next(sequences)
    except StopIteration:
        return init

    return accumulate_n_(
        op,
        [op(x, y) for x, y in zip(init, head)],
        sequences,
    )

def accumulate_n(op, init, sequences):
    return accumulate_n_(op, init, iter(sequences))

更清洁的命令:

代码语言:javascript
复制
def accumulate_n(op, init, sequences):
    for s in sequences:
        init = [op(x, y) for x, y in zip(init, s)]

    return init

最后,使用functools.reduce

代码语言:javascript
复制
def zipper(op):
    return lambda xs, ys: [op(x, y) for x, y in zip(xs, ys)]

def accumulate_n(op, init, sequences):
    return reduce(zipper(op), sequences, init)
票数 1
EN

Stack Overflow用户

发布于 2017-03-19 07:40:56

内置的zip函数结合使用*将列表解压缩为参数,可以方便地从序列序列中选择n个值:

代码语言:javascript
复制
def accumulate_n(op, init, sequences):
    if (not sequences) or (not sequences[0]):
        return type(sequences)()
    else:
        return [accumulate(op, init, i) for i in zip(*sequences)]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42883255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档