改組對象列表 - Shuffling a list of objects

問題:

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. 我覺得可使用random.shuffle方法,可是當列表中包含對象時,這彷佛失敗了。 Is there a method for shuffling objects or another way around this? 是否有一種用於改組對象的方法或解決此問題的另外一種方法? dom

import random

class A:
    foo = "bar"

a1 = a()
a2 = a()
b = [a1, a2]

print(random.shuffle(b))

This will fail. 這將失敗。 this


解決方案:

參考一: https://stackoom.com/question/468A/改組對象列表
參考二: https://oldbug.net/q/468A/Shuffling-a-list-of-objects
相關文章
相關標籤/搜索