简体   繁体   中英

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

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)
List2 = [List1[i] + List1[(i+1)%len(List1)] for i in range (len(List1))]
[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]])]  

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]

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]

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)

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 (ie 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!

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

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)

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))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM