擅长:python、mysql、java
<p>当您不介意使用外部库时,可以使用<a href="https://iteration-utilities.readthedocs.io/en/latest/generated/unique_everseen.html" rel="nofollow noreferrer">^{<cd1>}</a>。它类似于<code>set</code>,但允许使用<code>key</code>-函数并保留遇到的第一个值:</p>
<pre><code>>>> from iteration_utilities import unique_everseen
>>> from operator import itemgetter
>>> list(unique_everseen(zip(list_a, list_b), key=itemgetter(0)))
[('cat', 'c'), ('dog', 'c'), ('mouse', 'd')]
</code></pre>
<hr/>
<p>免责声明:<a href="https://github.com/MSeifert04/iteration_utilities" rel="nofollow noreferrer">^{<cd4>} package</a>是我写的。但是你可以在<a href="https://docs.python.org/library/itertools.html#itertools-recipes" rel="nofollow noreferrer">^{<cd5>} Recipes documentation</a>中找到一个几乎相等的配方</p>