Shuffling a list of objects

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

    The shuffling process is "with replacement", so the occurrence of each item may change! At least when when items in your list is also list.

    E.g.,

    ml = [[0], [1]] * 10
    

    After,

    random.shuffle(ml)
    

    The number of [0] may be 9 or 8, but not exactly 10.

提交回复
热议问题