Shuffling a list of objects

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

    import random
    
    class a:
        foo = "bar"
    
    a1 = a()
    a2 = a()
    a3 = a()
    a4 = a()
    b = [a1,a2,a3,a4]
    
    random.shuffle(b)
    print(b)
    

    shuffle is in place, so do not print result, which is None, but the list.

提交回复
热议问题