擅长:python、mysql、java
<p>您需要测试<em>全键</em>:</p>
<pre><code>(1, 'test') in x
</code></pre>
<p>元组键不是特殊的,使用元组作为键并不意味着元组中的两个元素都成为单独的键。</p>
<p>如果需要将<code>1</code>和<code>'test'</code>都作为键,则需要将它们分别添加为键,或者分别测试字典中的每个键。</p>
<p>您可以对<code>dict</code>类型进行子类化并对其进行扩展以支持您的解释:</p>
<pre><code>class tupledict(dict):
def __contains__(self, key):
if super(tupledict, self).__contains__(key):
return True
return any(key in k for k in self)
</code></pre>
<p>演示:</p>
<pre><code>>>> x = tupledict({(1, 'test'): ['marry', 'tom']})
>>> 1 in x
True
>>> (1, 'test') in x
True
</code></pre>