To select a random item from the list or shuffle the list, you can use the choice and shuffle functions from the random module of the standard library.
The random.choice() function returns a randomly selected element from the list.
>>> import random
>>> colors = ['red', 'blue', 'green', 'yellow']
>>> random.choice(colors)
'blue'
>>> random.choice(colors)
'green'
The random.shuffle() function reorders the elements in the list.
>>> cities = ['Etah', 'Kasganj', 'Dhampur', 'Najibabad', 'Bareilly', 'Chennai', 'Bangalore']
>>> random.shuffle(cities)
>>> cities
['Bangalore', 'Kasganj', 'Najibabad', 'Chennai', 'Bareilly', 'Etah', 'Dhampur']
>>> random.shuffle(cities)
>>> cities
['Bareilly', 'Najibabad', 'Chennai', 'Kasganj', 'Dhampur', 'Bangalore', 'Etah']
This function modifies the list in-place; it does not return a new list.
标签:functions,shuffle,Python,random,list,choice,Dhampur,cities From: https://www.cnblogs.com/zhangzhihui/p/18330948