samedi 19 juin 2021

How do you create a random list of integers, but exclude a certain number?

I'd like to create a random list of numbers in python, but exclude a certain number, k.

I can create a random list of integers: l = [random.randint(0,10) for i in range(5)] Then I can remove the number, k, if it's there, and add another random number in the range, but this seems like too many steps.

In researching this, It's quite easy to find out how to create a random number from a range and exclude a certain number, for example: print (choice([i for i in range(0,9) if i not in [2,5,7]])) I could do this the amount of times I need to create the array -- but this seems more complex than necessary.

Would appreciate some feedback this




Aucun commentaire:

Enregistrer un commentaire