Shuffling a list of objects

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

    #!/usr/bin/python3
    
    import random
    
    s=list(range(5))
    random.shuffle(s) # << shuffle before print or assignment
    print(s)
    
    # print: [2, 4, 1, 3, 0]
    

提交回复
热议问题