Shuffling a list of objects

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

    If you happen to be using numpy already (very popular for scientific and financial applications) you can save yourself an import.

    import numpy as np    
    np.random.shuffle(b)
    print(b)
    

    https://numpy.org/doc/stable/reference/random/generated/numpy.random.shuffle.html

提交回复
热议问题