Sum of consecutive pairs in a list including a sum of the last element with the first

怎甘沉沦 提交于 2020-08-04 07:45:50

问题


I have a list of elements like [1,3,5,6,8,7]. I want a list of sums of two consecutive elements of the list in a way that the last element is also added with the first element of the list. I mean in the above case, I want this list: [4,8,11,14,15,8]

But when it comes to the addition of the last and the first element during for loop, index out of range occurs. Consider the following code:

List1 = [1,3,5,6,8,7]
List2 = [List1[i] + List1[i+1] for i in range (len(List1))]

print(List2)

回答1:


List2 = [List1[i] + List1[(i+1)%len(List1)] for i in range (len(List1))]



回答2:


[List1[i] + List1[(i+1) % len(List1)] for i in range(len(List1))]

or

[sum(tup) for tup in zip(List1, List1[1:] + [List1[0]])]

or

[x + y for x, y in zip(List1, List1[1:] + [List1[0]])]  



回答3:


Because of the i+1, the index goes out of range

List1 = [1,3,5,6,8,7]
List2 = [List1[i-1] + List1[i] for i in range (len(List1))]

print (List2)

This way kinda works
Result:

[8, 4, 8, 11, 14, 15]



回答4:


What you're actually doing is adding consecutive items in the list, but when you reach the last index, your code still adds one to the last index, which results in an index out of range error. You should consider using:

List1 = [1,3,5,6,8,7]
List2 = [List1[i] + List1[i+1] for i in range (len(List1) - 1)]
List2.append(List1[0] + List1[-1])

print (List2)



回答5:


This rotates the list

In [9]: List1[1:] + List1[:1]                                                                 
Out[9]: [3, 5, 6, 8, 7, 1]

So following works perfectly

In [8]: [x + y for x, y in zip(List1, List1[1:] + List1[:1])]                                 
Out[8]: [4, 8, 11, 14, 15, 8]



回答6:


As you are running it to the len(List1) - 1 So when, i value will be len(List1) - 1, then i + 1 will be out of range (i.e. len(List1)), So you can try this solution:

List2 = [List1[i-1] + List1[i] for i in range (len(List1))]

Or you can do:

List2 = [List1[i] + List1[i+1] for i in range (len(List1)-1)]

Or you can use any other logic also. Cheers!




回答7:


Here's another ugly solution:

List1 = [1,3,5,6,8,7] 
List2 = [List1[i] + (List1+[List1[0]])[i+1] for i in range (len(List1))]

print (List2)



回答8:


Some more pythonic answers that weren't answered yet:

Using map can be more readable:

List2 = list(map(sum, zip(List1, List1[1:] + List1[:1])))

You can also use itertools to offset the list:

import itertools
List2 = list(map(sum, zip(List1, itertools.islice(itertools.cycle(List1), 1, None))))



回答9:


When i == 5, then it will enter the "if" statement and i is set to -1. Note that List[-1] is the same as List[len(List) - 1], which means that List[i+1] will become List[0], the first element of the list.

for i in range(len(List1)):
    if i == len(List1) - 1:
        i = -1
    List2.append(List1[i] + List1[i+1])


来源:https://stackoverflow.com/questions/63212888/sum-of-consecutive-pairs-in-a-list-including-a-sum-of-the-last-element-with-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!