Shuffling a list of objects

后端 未结 23 1724
眼角桃花
眼角桃花 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:52

    from random import random
    my_list = range(10)
    shuffled_list = sorted(my_list, key=lambda x: random())
    

    This alternative may be useful for some applications where you want to swap the ordering function.

提交回复
热议问题