How to start a for loop from the end of a vector, and at the value 0 do something

chrisis

I have to start looping a vector from its end to zero. When I meet the value 0 I need to replace it with the sum of the three previews values. The exception will be for the first zero met, I need just to sum the previous two values.

e.g.

v = [0, 5, 5, 0, 6, 6, 0, 7, 7 ]

Expected result:

v = [36, 5, 5, 26, 6, 6, 14, 7, 7 ]

Code

for i in range(len(v), 0):       
    if v == 0 :
        v[i] = v[i+1] + v[i+2] + v[i+3]

The code is not working. Where am I wrong?

Thomas Sablik

You can avoid index errors with sum(v[i+1:i+4]). You have to compare each element (if v[i] == 0) not the whole list.

v = [0, 5, 5, 0, 6, 6, 0, 7, 7 ]

for i in range(len(v) - 1, -1, -1):       
    if v[i] == 0 :
        v[i] = sum(v[i+1:i+4])
print(v)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

loop from end to start

From Dev

loop from end to start

From Dev

lodash : how to loop with between a start value and end value

From Dev

How to make a for loop to start from anywhere and end it same position?

From Dev

Loop from start to end to start in php

From Dev

Excel: how do I add one column from start to end with another one from end to start in excel?

From Dev

When to loop from 0 to end and when to loop from end to 0?

From Dev

How to get start day timestamp and end day timestamp from value

From Dev

Change value from 0 to something on tooltip only

From Dev

How to make in a for statement, "i" to start from a value until the cycle is end, and the second cycle to start from 1

From Dev

How to convert NA from factor vector to value of 0

From Dev

How to convert NA from factor vector to value of 0

From Dev

How do initialize an SIMD vector with a range from 0 to N?

From Dev

How to start counter from 0 rather than maximum value?

From Dev

How to end this kind of do loop?

From Dev

How to end this kind of do loop?

From Dev

How do I end this Loop

From Dev

How do I remove every value from a vector?

From Dev

Python : How to set a variable to something such as "None" or "0" when there is no value from the request

From Dev

how can i run the loop from start date to end date for every 3 months in php

From Dev

how can i run the loop from start date to end date for every 3 months in php

From Dev

How to end a loop, from outside the loop in python

From Dev

Problem subsetting vector from an index that doesn't start from 0

From Dev

how to do something at the end of a finger drag in libgdx/android?

From Dev

How do I add something to the end of "}" if a pattern is matched?

From Dev

How to start a job, do something different, and resume it again

From Dev

Delphi "for ... to" statement runs from end value to start value

From Dev

How to remove parentheses from JSON in start and end?

From Dev

How to get accurate time from start to end

Related Related

  1. 1

    loop from end to start

  2. 2

    loop from end to start

  3. 3

    lodash : how to loop with between a start value and end value

  4. 4

    How to make a for loop to start from anywhere and end it same position?

  5. 5

    Loop from start to end to start in php

  6. 6

    Excel: how do I add one column from start to end with another one from end to start in excel?

  7. 7

    When to loop from 0 to end and when to loop from end to 0?

  8. 8

    How to get start day timestamp and end day timestamp from value

  9. 9

    Change value from 0 to something on tooltip only

  10. 10

    How to make in a for statement, "i" to start from a value until the cycle is end, and the second cycle to start from 1

  11. 11

    How to convert NA from factor vector to value of 0

  12. 12

    How to convert NA from factor vector to value of 0

  13. 13

    How do initialize an SIMD vector with a range from 0 to N?

  14. 14

    How to start counter from 0 rather than maximum value?

  15. 15

    How to end this kind of do loop?

  16. 16

    How to end this kind of do loop?

  17. 17

    How do I end this Loop

  18. 18

    How do I remove every value from a vector?

  19. 19

    Python : How to set a variable to something such as "None" or "0" when there is no value from the request

  20. 20

    how can i run the loop from start date to end date for every 3 months in php

  21. 21

    how can i run the loop from start date to end date for every 3 months in php

  22. 22

    How to end a loop, from outside the loop in python

  23. 23

    Problem subsetting vector from an index that doesn't start from 0

  24. 24

    how to do something at the end of a finger drag in libgdx/android?

  25. 25

    How do I add something to the end of "}" if a pattern is matched?

  26. 26

    How to start a job, do something different, and resume it again

  27. 27

    Delphi "for ... to" statement runs from end value to start value

  28. 28

    How to remove parentheses from JSON in start and end?

  29. 29

    How to get accurate time from start to end

HotTag

Archive