Shuffling a list of objects

后端 未结 23 2006
眼角桃花
眼角桃花 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:47

    you can either use shuffle or sample . both of which come from random module.

    import random
    def shuffle(arr1):
        n=len(arr1)
        b=random.sample(arr1,n)
        return b
    

    OR

    import random
    def shuffle(arr1):
        random.shuffle(arr1)
        return arr1
    

提交回复
热议问题