Shuffling a list of objects

后端 未结 23 2208
眼角桃花
眼角桃花 2020-11-22 00:29

I have a list of objects and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is the

23条回答
  •  天命终不由人
    2020-11-22 00:59

    >>> import random
    >>> a = ['hi','world','cat','dog']
    >>> random.shuffle(a,random.random)
    >>> a
    ['hi', 'cat', 'dog', 'world']
    

    It works fine for me. Make sure to set the random method.

提交回复
热议问题