擅长:python、mysql、java
<p>你能给对象类型添加<a href="https://docs.python.org/2/reference/datamodel.html#object.__hash__" rel="nofollow">^{<cd1>}</a>和<code>__eq__()</code>方法吗?如果是这样,可以直接使用集合:</p>
<pre><code>set(myobj).intersection(objects)
</code></pre>
<p>即使不能使用<code>__hash__()</code>,也可以使用以下键:</p>
<pre><code>subset_keys = set(o.key for o in myobj).intersection(o.key for o in objects)
subset = [ o for o in objects if o.key in subset_keys ]
</code></pre>
<p>更新:根据更新的问题。。。你知道吗</p>
<pre><code>class object:
def __init__(self, key, x, y):
self.key = key
self.x = x
self.y = y
def __hash__(self):
# must return an int
return self.key
def __eq__(self, other):
return hash(self) == hash(other)
</code></pre>