擅长:python、mysql、java
<p>在使用<code>element = t1,t2</code>这个语句时,您正在生成元组的元组。示例:-你知道吗</p>
<pre><code>>>> x=(3,4)
>>> y=(7,3)
>>> z=x,y
>>> z
((3, 4), (7, 3))
</code></pre>
<p>调用<code>for x in element:</code>意味着x值将是t1或t2,而不是t1和t2的元素。你知道吗</p>
<p>如果x=t1,那么调用If语句<code>x in t1 and x in t2</code>将检查<code>if t1 is a element of t1</code>和<code>if t1 is a element of t2</code>,这是不可能的,因为两者都是元组而不是元组的元组。所以你的if语句不会被调用。所以打印将永远不会被执行。<br/>
同样的情况也会发生在x=t2</p>
<p>您可以使用<code>Counter</code>from <code>collections</code>来获得这样的交叉点:</p>
<pre><code>>>> from collections import Counter
>>> a = Counter((1, 2, 3, 4, 4, 5, 5))
>>> b = Counter((4, 4, 5, 6, 7, 8))
>>> tuple(a & b)
(4,5)
</code></pre>