Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I subtract each element by one to generate a list contains all zeros i tried to solve it in recursive way

if I have a list how can I subtract each element by one in a recursive way ??I implemented this code

list=[5,4,3,2,1,1]
level=numbers[0]

def list_arrays(numb):
    if len(numbers)-1 < level:
      print("nograph")
   else:
     for i in range(len(numbers)):
       numbers[i] = numbers[i] - 1
         print(numbers)

the output should be like this

> [4,3,2,1,0,0]
> [3,2,1,0,0,0]
> [2,1,0,0,0,0]
> [1,0,0,0,0,0]

but its printed only the first array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> [4,3,2,1,0,0]

>Solution :

import numpy as np

l=[5,4,3,2,1,1]
arr = np.array(l)

# loop until all elements are 0
while not np.all((arr == 0)):
    for i in range(len(arr)):
        # subtract non-zero element by 1
        if not arr[i] == 0:
            arr[i] -= 1 
    print(arr)

output:

> [4 3 2 1 0 0] 
> [3 2 1 0 0 0] 
> [2 1 0 0 0 0] 
> [1 0 0 0 0 0] 
> [0 0 0 0 0 0]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading