如何减少列表中的列表

发布于 2025-02-12 18:34:38 字数 814 浏览 0 评论 0原文

我的直觉说降低(从功能上的导入降低)会提供我想要的东西,但我无法在这里使用它来缠绕我的头。

def test_reduce_lists_by_summing_them():
    """
    Sum each item from the first list with the same positional item from the subsequent list. 
    The result is the next first list and appended to the returned result list.
    """
    input_ = [[1, 0, 0], [0, 1, 0], [0, 0, 2], [3, 0, 0]]
    expected_output = [[1, 1, 0], [1, 1, 2], [4, 1, 2]]

    def f(x, y):
        """Return the sum of two positional items"""
        return x + y

    output = []
    for group in zip(input_[0:], input_[1:]):
        # Obvisously not working since the first list is not generated but just taken grp[0]
        output.append(list(map(f, group[0], group[1])))
    assert output == expected_output

My gut says that reduce (from functools import reduce) would deliver what I'm looking for, yet I can't wrap my head around using it here.

def test_reduce_lists_by_summing_them():
    """
    Sum each item from the first list with the same positional item from the subsequent list. 
    The result is the next first list and appended to the returned result list.
    """
    input_ = [[1, 0, 0], [0, 1, 0], [0, 0, 2], [3, 0, 0]]
    expected_output = [[1, 1, 0], [1, 1, 2], [4, 1, 2]]

    def f(x, y):
        """Return the sum of two positional items"""
        return x + y

    output = []
    for group in zip(input_[0:], input_[1:]):
        # Obvisously not working since the first list is not generated but just taken grp[0]
        output.append(list(map(f, group[0], group[1])))
    assert output == expected_output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-02-19 18:34:38

您正在寻找的是累积

from itertools import accumulate

# perhaps, convert it to a list explicitly
accumulate(input_, lambda x, y: list(map(sum, zip(x, y))))

# a more explicit equivalent:
list(accumulate(input_, lambda x, y: [x_+y_ for x_, y_ in zip(x, y)]))

如果您'愿意使用numpy,它将很简单:

np.array(input_).cumsum(axis=0)

upd demo:

In [36]: list(accumulate(input_, lambda x, y: list(map(sum, zip(x, y)))))
Out[36]: [[1, 0, 0], [1, 1, 0], [1, 1, 2], [4, 1, 2]]

In [37]: list(accumulate(input_, lambda x, y: [x_+y_ for x_, y_ in zip(x, y)]))
Out[37]: [[1, 0, 0], [1, 1, 0], [1, 1, 2], [4, 1, 2]]

In [38]: np.array(input_).cumsum(axis=0)
Out[38]: 
array([[1, 0, 0],
       [1, 1, 0],
       [1, 1, 2],
       [4, 1, 2]])

What you're looking for is accumulate:

from itertools import accumulate

# perhaps, convert it to a list explicitly
accumulate(input_, lambda x, y: list(map(sum, zip(x, y))))

# a more explicit equivalent:
list(accumulate(input_, lambda x, y: [x_+y_ for x_, y_ in zip(x, y)]))

And if you're willing to use numpy, it would be as simple as:

np.array(input_).cumsum(axis=0)

UPD Demo:

In [36]: list(accumulate(input_, lambda x, y: list(map(sum, zip(x, y)))))
Out[36]: [[1, 0, 0], [1, 1, 0], [1, 1, 2], [4, 1, 2]]

In [37]: list(accumulate(input_, lambda x, y: [x_+y_ for x_, y_ in zip(x, y)]))
Out[37]: [[1, 0, 0], [1, 1, 0], [1, 1, 2], [4, 1, 2]]

In [38]: np.array(input_).cumsum(axis=0)
Out[38]: 
array([[1, 0, 0],
       [1, 1, 0],
       [1, 1, 2],
       [4, 1, 2]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文