如何在具有相同索引的几个列表中添加数字?

时间:2018-04-14 04:06:54

标签: python arrays list indexing zip

这就是我从2d列表中删除的列表:

[0, 0, 0, 0, 0, 3, 3, 3]

[0, 0, 0, 0, 2, 0, 0, 0, 2]

[0, 0, 0, 1, 0, 0, 0, 0, 0, 1]

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1]

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, -2]

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -3, -3]

因为它们在同一个列表中被删除,所以我不知道如何将它们添加到新列表中。

我是否将它们添加到新列表中并压缩它们?但是我如何追加它们(因为它们来自同一个列表,如果我使用索引[3],它只会出现每行中的数字(例如,0表示索引[3],line1))

这就是我的期望:

[ 0, 0, 0, 1, 2, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -2, -1, 0, 0, 0 ]

也就是说,所有具有相同索引的数字都会加在一起。

请给我一些提示。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用itertools.zip_longest

  

创建一个聚合来自每个迭代的元素的迭代器。如果迭代的长度不均匀,则使用 fillvalue 填充缺失值。迭代继续,直到最长的可迭代用尽。

from itertools import zip_longest

num_lists = [[0, 0, 0, 0, 0, 3, 3, 3],
             [0, 0, 0, 0, 2, 0, 0, 0, 2],
             [0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, -2],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -3, -3]]

result = [sum(lst) for lst in zip_longest(*num_lists, fillvalue=0)]

print(result)

[0, 0, 0, 1, 2, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -2, -1, 0, 0, 0]
>>> 

答案 1 :(得分:0)

welp,从示例中可以看出,转到目标列表的项目不会与源列表中的指标重叠 - 如果保证您可以执行以下操作:

list_of_lists = [ # broken list 1
    [[0, 0, 0, 0, 0, 3, 3, 3],
     [0, 0, 0, 0, 2, 0, 0, 0, 2],
     [0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, -2],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -3, -3]],
   [# broken list 1
    [0, 0, 0, 0, 0, 3, 3, 3],
    [0, 0, 0, 0, 2, 0, 0, 0, 2],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, -2],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, -3, -3]],
  ]


def combine_broken_list(lists):
    max_size = max(map(len, lists))  # get target list size
    result = [0] * max_size          # initialize target list to zeros - if nothing target is zero, then this will stay that way

    for sublist in lists:
         found_ix = 0                     # when we find a nonzero, we know we do not need to look before it again, since we start at 0.
        for i, x in enumerate(sublist[found_ix:]):  
            if x != 0:                   # iterate and find non zeros
                found_ix = i
                result[i] = sublist[i]   # set target value to source value
     return result

# run on several broken lists
result = list(map(combine_broken_list, list_of_lists))

print(result) # show result
# test result 1
tgt = [0, 0, 0, 1, 2, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -2, -1, 0, 0, 0 ]
print(result[0] == tgt)
# >True

如果该保证不存在,那么什么是优先规则?

对于numpy,可能有一个更有效的解决方案,但这应该有效。