mercredi 16 octobre 2019

Create a list of random numbers and filter the list to only have numbers larger than 50

I am using list comprehension to create a list of random numbers with numpy. Is there a way to check if random number generated is larger than 50 and only then append it to the list.

I know I can simply use:

numbers = [np.random.randint(50,100) for x in range(100)]

and that would solve the issue, but I just want to know if its possible to somehow check if np.random.randint(1,100) generated number greater than 50

Something like

numbers = [np.random.randint(1,100) for x in range(100) if {statement}]

Because comparing np.random.randit generates another number which is not the same as the first one.

I just want to know if there is a possibility to filter generated numbers before adding them to a list.




Aucun commentaire:

Enregistrer un commentaire