这有一个ordered set(可能是new link)的配方,参考Python 2 Documentation.这在Py2上运行.6或更高版本和3.0或更高版本,无需任何修改.该接口几乎与正常设置完全相同,只是初始化应该使用列表完成.
OrderedSet([1, 2, 3])
这是一个MutableSet,所以.union
的签名与Set的签名不匹配,但是由于它包括__or__
,所以可以很容易地添加一些类似的东西:
@staticmethod
def union(*sets):
union = OrderedSet()
union.union(*sets)
return union
def union(self, *sets):
for set in sets:
self |= set