擅长:python、mysql、java
<p>您可以将列表转换为<a href="https://docs.python.org/3/library/stdtypes.html#set" rel="nofollow noreferrer">^{<cd1>}</a>,并使用<a href="https://docs.python.org/3/library/stdtypes.html#frozenset.intersection" rel="nofollow noreferrer">^{<cd2>}</a>对其执行<a href="https://en.wikipedia.org/wiki/Intersection_(set_theory)" rel="nofollow noreferrer"><em>intersection</em></a>:</p>
<pre><code>>>> a = ['ab','ac','ad','aba','abc']
>>> b = ['ab', 'ac', 'ab']
>>> set(a) & set(b)
{'ab', 'ac'}
</code></pre>
<p><em>集合交集</em>返回两个集合中通用的元素。但是请注意:由于<code>set</code>是无序的,如果需要,您将无法维护列表<code>a</code>或<code>b</code>中的顺序</p>
<p>下面是使用<a href="https://docs.python.org/3/library/stdtypes.html#frozenset.intersection" rel="nofollow noreferrer">^{<cd6>}</a>方法实现相同功能的函数方法:</p>
<pre><code>>>> set(a).intersection(b)
{'ac', 'ab'}
</code></pre>