【问题标题】:How do I add the numbers in a few list with same index?如何在具有相同索引的几个列表中添加数字?
【发布时间】:2018-04-14 04:06:54
【问题描述】:

这就是我从二维列表中分解出来的列表:

[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]

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

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

这是我所期望的:

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

也就是说,所有具有相同索引的数字相加。

请给我一些提示。谢谢!

【问题讨论】:

  • 你能把列表的代码贴出来,这样我们就可以准确地看到它们的格式

标签: python arrays list indexing zip


【解决方案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]
>>> 

【讨论】:

  • 欣赏!如果我有多个要添加的列表,我可以使用它吗?
【解决方案2】:

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 可能有一个更有效的解决方案,但这应该可行。

【讨论】:

  • 还有一个问题——如果我有多个列表要像上面那样将它们添加在一起,我是否在此之后使用 zip 功能?
  • 仅当它们的长度相同时。否则我只会将其作为函数运行,然后将结果附加到新列表:
  • 谢谢!我还没有走那么远,我会尝试一下。感谢您的耐心等待!
  • 你的意思是这样吗?更新代码以反映对几个损坏的二维列表执行此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多