Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表中的一个元素添加到下一个元素,然后添加到下一个元素,等等_Python_List - Fatal编程技术网

Python 将列表中的一个元素添加到下一个元素,然后添加到下一个元素,等等

Python 将列表中的一个元素添加到下一个元素,然后添加到下一个元素,等等,python,list,Python,List,下面是一个简单列表中的示例 mylist = [2,5,9,12,50] 我想将第一个元素(在本例中为2)添加到它旁边的元素。是5号。结果(2+5=7)应该添加到下一个元素中,在我的示例中是9。结果应添加到下一个元素中,等等 我现在有一段代码正在运行,但必须有一个更简单更好的方法: newlist = [5, 9, 12 , 50] counts = 0 a = 2 while (counts < 5): a = a + mylist[n] print a cou

下面是一个简单列表中的示例

mylist = [2,5,9,12,50]
我想将第一个元素(在本例中为2)添加到它旁边的元素。是5号。结果(2+5=7)应该添加到下一个元素中,在我的示例中是9。结果应添加到下一个元素中,等等

我现在有一段代码正在运行,但必须有一个更简单更好的方法:

newlist = [5, 9, 12 , 50]
counts = 0
a = 2
while (counts < 5):
    a = a + mylist[n]
    print a
    counts = counts + 1
下一段:

mylist = [2, 5, 9, 12, 50]
lines_of_file = [4, 14, 20, 25, 27]
sum_list = []
outcome = 0

for element in mylist:
    outcome = outcome + element
    sum_list.append(outcome)

fopen = ('test.txt', 'r+')
write = fopen.readlines()

for element, line in zip(sum_list, lines_of_file):
    write[line] = str(element)

fopen.writelines()
fopen.close()
用于将序列的所有值相加:

>>> sum([2,5,9,12,50])
78
但如果您想保持一个连续的总数,只需循环查看您的列表:

total = 2
for elem in newlist:
    total += a
    print total
也可用于构建列表:

total = 2
cumsum = []
for elem in newlist:
    total += a
    cumsum.append(total)

您可以这样做:

>>> mylist = [2,5,9,12,50]
>>> 
>>> total = 0  # initialize a running total to 0
>>> for i in mylist:  # for each i in mylist
...     total += i  # add i to the running total
...     print total  # print the running total
... 
2
7
16
28
78

有一个很好的功能来实现这一点,即:


您可以使用
list(…)
将数组转换回列表。

以下是一种可能的解决方案:

#!/usr/local/bin/python2.7

mylist = [2,5,9,12,50]
runningSum = 0

for index in range(0,len(mylist)):
    runningSum = runningSum + mylist[index]
    print runningSum

如果要将其写入文件中的特定位置,请尝试以下操作:

假设您有一个
numbers.txt
文件,有10行,如下所示:

0
0
0
0
0
0
0
0
0
0
然后用这个:

original_list = [1, 3, 5, 7, 9]
lines_to_write = [2, 4, 6, 8, 10]  # Lines you want to write the results in
total = 0
sum_list = list()

# Get the list of cumulative sums
for element in original_list:
    total = total + element
    sum_list.append(total)
# sum_list = [1, 4, 9, 16, 25]

# Open and read the file
with open('numbers.txt', 'rw+') as file:
    file_lines = file.readlines()
    for element, line in zip(sum_list, lines_to_write):
        file_lines[line-1] = '{}\n'.format(element)
    file.seek(0)
    file.writelines(file_lines)
然后您会得到
numbers.txt
,如下所示:

0
1
0
4
0
9
0
16
0
25

以下是另一个版本:

newlist = [2,5,9,12,50]

for i in range(1,len(newlist)):
    print sum(newlist[:i+1])
请注意,它不包括您想要的
2
。 不过,它可能会一次又一次地计算总和(我不知道编译器有多聪明)

输出:

7
16
28
78

如果不想显示列表中的第一项(编号2),则此解决方案将执行以下操作:

mylist = [2,5,9,12,50]

it = iter(mylist)
total = it.next() # total = 2, first item
for item in it:
    total += item
    print total

看起来OP需要一个累积和。@arshajii:那么,到目前为止,如果不向和中添加下一个值,那么什么是累积和?他希望在每个阶段显示该值。问题已解决,非常感谢……几分钟后答案将被接受。对不起,我是编程新手。我需要将每个结果都写入一个文件中。您使用哪种Python版本你在吗?是的,这正是我想要的。我尝试了一个for循环。但似乎我这样做是为了使复杂化这也是一个很好的解决方案。但是如果我想将值(例如第10、12、15、17和20行)写入一个文件中呢?@Phil我不确定我是否理解你的要求。你说你想建立累积和列表,但只在一个文件中写入一些结果?抱歉@Gabe我编辑了我的代码。如果你愿意,可以看一下。如果是,我希望这个在for循环中possible@Phil我刚刚编辑了anwer我想你在找Hanks Gabe,但我想我该怎么做。我在for循环中使用了zip。我再次编辑了代码,它工作了!!
7
16
28
78
mylist = [2,5,9,12,50]

it = iter(mylist)
total = it.next() # total = 2, first item
for item in it:
    total += item
    print total