改组对象列表

℡╲_俬逩灬. 提交于 2020-08-05 06:19:52

问题:

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? 是否有一种用于改组对象的方法或解决此问题的另一种方法?

import random

class A:
    foo = "bar"

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

print(random.shuffle(b))

This will fail. 这将失败。


解决方案:

参考一: https://stackoom.com/question/468A/改组对象列表
参考二: https://oldbug.net/q/468A/Shuffling-a-list-of-objects
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!