How to randomly select an item from a list?

后端 未结 15 1693
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 07:49

Assume I have the following list:

foo = [\'a\', \'b\', \'c\', \'d\', \'e\']

What is the simplest way to retrieve an item at random from thi

15条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 08:18

    This may already be an answer but you could use random.shuffle. Example:

    import random
    foo = ['a', 'b', 'c', 'd', 'e']
    random.shuffle(foo)
    

提交回复
热议问题