【问题标题】:How do you sum together and merge multiple numbers in a list? (Python)您如何将列表中的多个数字相加并合并? (Python)
【发布时间】:2020-11-30 12:53:55
【问题描述】:

所以,假设我有以下列表和变量 i:

data = [ [1, 2, 3, 1, 1], [1, 2, 3, 0, 0], [1, 2, 3, 2, 1] ]
i = 3

我想创建一个新列表,该列表将汇总并合并每个子列表中第 i 个元素向上的数字以生成以下内容:

new_data = [ [1, 2, 5], [1, 2, 3], [1, 2, 6] ]

我从每个子列表中向上求和第三个元素并将这些元素合并在一起。我想问一下,通过使用 Python 中的标准库,我可以为 i 的任意(整数)值实现这一点。我有以下想法,但我不确定如何将其放入真正的 Python 代码中:

(伪代码)

new_data = []
for sublist in data:
      new_data.append(elements before the ith element)
      new_data.append(ith element onwards summed together)

【问题讨论】:

  • 相关问题 - stackoverflow.com/questions/30247666/…。但我的问题涉及任意元素的数字和合并元素。
  • 您能否用不同的词解释“将每个子列表中的数字相加并合并第i个元素向上”的含义?即使有这个例子,我也不明白你的意思。您能否还包括一个不同的例子?在此示例中,所有三个列表都相同可能无济于事。
  • 感谢您告诉我,我已经更新了我的示例,因此每个子列表都不同。现在应该更清楚了。我的意思是我想将多个元素加在一起并将该总和表示为单个元素。如果您需要任何进一步的说明,请告诉我。

标签: python list math


【解决方案1】:

您可以按索引对内部列表进行切片,然后将其余部分与单行列相加:

>>> new_data = [row[:i-1] + [sum(row[i-1:])] for row in data]
>>> new_data
[[1, 2, 5], [1, 2, 5], [1, 2, 5]]

【讨论】:

    【解决方案2】:

    您可以在taras' answer 中找到一个不错的“pythonic”单行:

    new_data = [row[:i-1] + [sum(row[i-1:])] for row in data].

    从伪代码到正确的python代码

    我的回答重点是帮助您将伪代码转换为 python 代码。

    伪代码:

    new_data = []
    for sublist in data:
          new_data.append(elements before the ith element)
          new_data.append(ith element onwards summed together)
    

    您的伪代码的第一个问题是您要区分列表 data 及其子列表 sublist,但您没有区分列表 new_data 及其子列表。让我在循环中添加一个变量new_sublist

    new_data = []
    for sublist in data:
          new_sublist = []
          new_sublist.append(elements before the ith element)
          new_sublist.append(ith element onwards summed together)
          new_data.append(new_sublist)
    

    您的伪代码的第二个问题:您在循环的每次迭代中对.append 进行两次调用。但是,这两个调用并不相似:第一个调用应该附加几个元素,而第二个调用应该附加一个元素。 Python 对这两种操作进行了区分;如果您想一次添加多个元素,请使用.extend 而不是.append。代码变为:

    new_data = []
    for sublist in data:
          new_sublist = []
          new_sublist.extend([elements before the ith element])
          new_sublist.append(ith element onwards summed together)
          new_data.append(new_sublist)
    

    最后,我们可以将您的伪代码转换为正确的 python 代码,使用列表切片来获取第 i 个元素之前的元素,并使用内置函数 sum 以及列表切片来对第 i 个元素进行求和:

    new_data = []
    for sublist in data:
          new_sublist = []
          new_sublist.extend(sublist[:i])
          new_sublist.append(sum(sublist[i:]))
          new_data.append(new_sublist)
    

    请注意,在 python 中,循环遍历一个列表以构造一个新列表是一种非常常见的操作,我们使用紧凑而优雅的列表解析语法来代替多行 for-loop:

    new_data = [sublist[:i] + [sum(sublist[i:])] for sublist in data]
    

    相关文档

    【讨论】:

      【解决方案3】:

      你可以这样做:

      def merger(data, pos):
          pos -= 1 # 0 based, your i above is 1 based
      
          # collection list for result
          result = []
          # go over each given inner part
          for inner in data:
              # add it up to the correct position
              result.append(inner[:pos])
              # extend it by the sum of the remainder values
              result[-1].append(sum(inner[pos:]))
      
          return result
      
      data = [ [1, 2, 3, 1, 1], [1, 2, 3, 1, 1], [1, 2, 3, 1, 1] ]
      i = 3
      
      print(merger(data,i)) 
      

      输出:

      [[1, 2, 5], [1, 2, 5], [1, 2, 5]]
      

      或者简而言之:

      def merger(data, pos):
          return [[inner[: pos - 1] + [sum(inner[pos - 1 :])] for inner in data]]
      

      【讨论】:

        【解决方案4】:
        data = [ [1, 2, 3, 1, 1], [1, 2, 3, 1, 1], [1, 2, 3, 1, 1] ]
        i = 3
        new_data = [sublist[:i-1] + [sum(sublist[i-1:])] for sublist in data]
        print(new_data)
        >>> [[1, 2, 5], [1, 2, 5], [1, 2, 5]]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-12
          • 1970-01-01
          • 2021-07-17
          • 2022-11-14
          • 2014-06-20
          相关资源
          最近更新 更多