如何将多个数字相加并合并到一个列表中?(Python)
假设我有以下列表和变量I:如何将多个数字相加并合并到一个列表中?(Python),python,list,math,Python,List,Math,假设我有以下列表和变量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值。我有以下想法,
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代码中:
(伪代码)
你可以这样做:
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]]
你可以这样做:
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]]
您可以按索引对内部列表进行切片,并用一行代码对其余列表求和:
>>> new_data = [row[:i-1] + [sum(row[i-1:])] for row in data]
>>> new_data
[[1, 2, 5], [1, 2, 5], [1, 2, 5]]
您可以按索引对内部列表进行切片,并用一行代码对其余列表求和:
>>> new_data = [row[:i-1] + [sum(row[i-1:])] for row in data]
>>> new_data
[[1, 2, 5], [1, 2, 5], [1, 2, 5]]
您可以在以下列表中找到一个很好的“pythonic”单行线:
new_data=[row[:i-1]+[sum(row[i-1:])]表示数据中的行]
从伪代码到正确的python代码
我将重点回答如何帮助您将伪代码转换为python代码
伪代码:
new_data = []
for sublist in data:
new_data.append(elements before the ith element)
new_data.append(ith element onwards summed together)
伪代码的第一个问题是,您正在区分列表数据
及其子列表子列表
,但没有区分列表新数据
及其子列表。让我在循环中添加一个变量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=[]
对于数据中的子列表:
新的_子列表=[]
新建_子列表。扩展(子列表[:i])
新的_子列表.append(sum(子列表[i:]))
新建\u数据.append(新建\u子列表)
请注意,在python中,在列表上循环以构造新列表是一种常见的操作,因此我们使用紧凑而优雅的列表理解语法来完成此操作,而不是使用多行for
-loop:
new_data=[sublist[:i]+[sum(sublist[i:])]表示数据中的子列表]
相关文件
- )李>
- )李>
- )李>
new_data=[row[:i-1]+[sum(row[i-1:])]表示数据中的行]
从伪代码到正确的python代码
我将重点回答如何帮助您将伪代码转换为python代码
伪代码:
new_data = []
for sublist in data:
new_data.append(elements before the ith element)
new_data.append(ith element onwards summed together)
伪代码的第一个问题是,您正在区分列表数据
及其子列表子列表
,但没有区分列表新数据
及其子列表。让我在循环中添加一个变量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=[]
对于数据中的子列表:
新的_子列表=[]
新建_子列表。扩展(子列表[:i])
新的_子列表.append(sum(子列表[i:]))
新建\u数据.append(新建\u子列表)
请注意,在python中,在列表上循环以构造新列表是一种常见的操作,因此我们使用紧凑而优雅的列表理解语法来完成此操作,而不是使用多行for
-loop:
new_data=[sublist[:i]+[sum(sublist[i:])]表示数据中的子列表]
相关文件
- )李>
- )李>
- )李>