擅长:python、mysql、java
<p>这里有一个<a href="http://code.activestate.com/recipes/576694/" rel="noreferrer">ordered set</a>(可能是<a href="https://github.com/ActiveState/code/blob/3b27230f418b714bc9a0f897cb8ea189c3515e99/recipes/Python/576696_OrderedSet_with_Weakrefs/README.md" rel="noreferrer">new link</a>)的配方,它是从<a href="https://docs.python.org/2/library/collections.html" rel="noreferrer">Python 2 Documentation</a>中引用的。这在Py2.6或更高版本和3.0或更高版本上运行,无需任何修改。这个接口几乎和一个普通的集合完全一样,只是初始化应该用一个列表来完成。</p>
<pre><code>OrderedSet([1, 2, 3])
</code></pre>
<p>这是一个可变集,因此<code>.union</code>的签名与set的签名不匹配,但是由于它包含<code>__or__</code>,可以很容易地添加类似的内容:</p>
<pre><code>@staticmethod
def union(*sets):
union = OrderedSet()
union.union(*sets)
return union
def union(self, *sets):
for set in sets:
self |= set
</code></pre>