Shuffling a list of objects

后端 未结 23 1681
眼角桃花
眼角桃花 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 01:04

    For one-liners, userandom.sample(list_to_be_shuffled, length_of_the_list) with an example:

    import random
    random.sample(list(range(10)), 10)
    

    outputs: [2, 9, 7, 8, 3, 0, 4, 1, 6, 5]

提交回复
热议问题