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

How to end this kind of do loop?

From Dev

How to convert NA from factor vector to value of 0

From Dev

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

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

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

From Dev

How to start counter from 0 rather than maximum value?

From Dev

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

From Dev

How to remove parentheses from JSON in start and end?

From Dev

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

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

loop from end to start

From Dev

How to end this kind of do loop?

From Dev

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

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 do I add something to the end of "}" if a pattern is matched?

From Dev

How to convert NA from factor vector to value of 0

From Dev

Loop from start to end to start in php

From Dev

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

From Dev

How do I end this Loop

From Dev

How to get accurate time from start to end

From Dev

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

From Dev

how can i run the loop from start date to end date for every 3 months 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

How do I remove every value from a vector?

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 a for loop to start from anywhere and end it same position?

Related Related

  1. 1

    loop from end to start

  2. 2

    How to end this kind of do loop?

  3. 3

    How to convert NA from factor vector to value of 0

  4. 4

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

  5. 5

    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

  6. 6

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

  7. 7

    How to start counter from 0 rather than maximum value?

  8. 8

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

  9. 9

    How to remove parentheses from JSON in start and end?

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

    loop from end to start

  14. 14

    How to end this kind of do loop?

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

    How to convert NA from factor vector to value of 0

  19. 19

    Loop from start to end to start in php

  20. 20

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

  21. 21

    How do I end this Loop

  22. 22

    How to get accurate time from start to end

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

    How do I remove every value from a vector?

  27. 27

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

  28. 28

    Change value from 0 to something on tooltip only

  29. 29

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

HotTag

Archive