vendredi 23 avril 2021

Keep reducing values in a list until all of them are 0

I have an initial list called origin_list. I want to pick a position in this list at random and reduce the value of that positional element by 1 until all of them are 0. What would the fastest way to do this be?

I have some rather inaccurate pseudo-code below to get an idea of what I'm trying to do

import numpy as np
origin_list = [120,240,201]
while all(origin_list)>=0:
    rand_int = np.random.randint(0,len(origin_list))
    origin_list[rand_int] = origin_list[rand_int]-1
    if origin_list[rand_int]==0:
    # Continue with reducing only the non-zero elements in the list




Aucun commentaire:

Enregistrer un commentaire