代码之家  ›  专栏  ›  技术社区  ›  VERBOSE

如何将每个嵌套列表的总和中继到下一个列表?

  •  3
  • VERBOSE  · 技术社区  · 1 年前

    我的输入是以下列表:

    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    

    我需要对嵌套列表求和 i 并将其填充到列表的右侧 i+1 ,就像接力赛。

    我不得不提一下,原始清单应该原封不动,我不允许复制它。

    wanted = [[3, 4, -1], [0, 1, 6], [-2, 7], [7, 5, 8, 5]]
    

    我尝试了下面的代码,但我得到了一个包含比原来更多元素的列表:

    from itertools import pairwise
    
    wanted = []
    
    for left, right in pairwise(my_list):
        wanted.extend([left, right + [sum(left)]])
        
    print(wanted)
    [[3, 4, -1], [0, 1, 6], [0, 1], [-2, 1], [-2], [7, 5, 8, -2]]
    

    你们能解释一下发生了什么吗?或者提出另一个解决方案?

    6 回复  |  直到 1 年前
        1
  •  3
  •   Andrej Kesely    1 年前

    你可以试试 itertools.accumulate :

    from itertools import accumulate
    
    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    
    print(list(accumulate(my_list, lambda l1, l2: [*l2, sum(l1)])))
    

    打印:

    [[3, 4, -1], [0, 1, 6], [-2, 7], [7, 5, 8, 5]]
    

    编辑:正如@KellyBundy在评论中指出的,列表中的第一个元素是共享的。因此,如果有问题,请从中复制,例如:

    accumulate([my_list[0][:], *my_list[1:]], lambda l1, l2: [*l2, sum(l1)])
    
        2
  •  2
  •   Kelly Bundy    1 年前

    列出comp:

    new_list = [
        lst
        for add in [[]]
        for lst in my_list
        for lst in [lst + add]
        for add in [[sum(lst)]]
    ]
    

    Attempt This Online!

        3
  •  1
  •   nate-thegrate    1 年前
    def relay(l: list[list[int]]):
        for i in range(1, len(l)):
            l[i].append(sum(l[i - 1]))
        return my_list
    
    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    
    print(relay(my_list))
    

    这将产生您想要的输出。

        4
  •  1
  •   S.B Daniel Pryden    1 年前

    您可以使用 pairwise 方法如下:

    from itertools import pairwise
    from copy import deepcopy
    
    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    new_list = deepcopy(my_list)
    
    for lst_a, lst_b in pairwise(new_list):
        lst_b.append(sum(lst_a))
    
    print(new_list)
    

    或者你可以试试这个:

    from copy import deepcopy
    
    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    new_list = deepcopy(my_list)
    
    for i in range(len(new_list) - 1):
        new_list[i + 1].append(sum(new_list[i]))
    
    print(new_list)
    

    输出:

    [[3, 4, -1], [0, 1, 6], [-2, 7], [7, 5, 8, 5]]
    
        5
  •  0
  •   Enes    1 年前

    我试了一点,能够写出这样的东西,我希望它能有所帮助:

    my_list = [[3, 4, -1], [0, 1], [-2], [7, 5, 8]]
    
    length = len(my_list)
    
    wanted = []
    for i in range(0,length):    # create new 2d list
        wanted.append([])
    
    prev_total = 0    # store row_total for next iteration
    row_total = 0
    
    for i in range(0,length):
        prev_total = row_total
        row_total = 0
        for j in range(0,len(my_list[i])):
            wanted[i].append(my_list[i][j])
            row_total += my_list[i][j]
    
        if i != 0:
            wanted[i].append(prev_total)
            row_total += prev_total
    
    print(wanted)
    
        6
  •  0
  •   Swifty Mr Fooz    1 年前

    我只需要把我自己的理解列表添加到其他答案中:

    from itertools import accumulate
    [sublist + ([s] if s is not None else []) for sublist, s in zip(my_list, [None, *accumulate(map(sum, my_list))])]
    

    注: ([s] if s is not None else []) 可以“缩短”为 [s] * (s is not None)