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