Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
转载于:https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
转载于:https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list
If you also need the index, use random.randrange
from random import randrange
random_index = randrange(len(foo))
print(foo[random_index])