<h2>性能比较</h2>
<p>比较<em>python3.9.1</em>和<em>python2.7.16</em>上这里提到的所有答案的性能</p>
<h3><em>Python 3.9.1</em></h3>
<p>答案按性能顺序列出:</p>
<ol>
<li><p><em><a href="https://stackoverflow.com/a/4211239/2063361">Arkku's</a><strong><code>set</code>使用减法进行差分</strong>“-”运算-(每个循环91.3纳秒)</em></p>
<pre><code>mquadri$ python3 -m timeit -s "l1 = set([1,2,6,8]); l2 = set([2,3,5,8]);" "l1 - l2"
5000000 loops, best of 5: 91.3 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>使用<code>set().difference()</code></strong>-(每个环路133毫微秒)</em></p>
<pre><code>mquadri$ python3 -m timeit -s "l1 = set([1,2,6,8]); l2 = set([2,3,5,8]);" "l1.difference(l2)"
2000000 loops, best of 5: 133 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>基于<code>set</code>查找的列表理解</strong>-(每个循环366nsec)</em></p>
<pre><code> mquadri$ python3 -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "[x for x in l1 if x not in l2]"
1000000 loops, best of 5: 366 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/4211228/2063361">Donut's</a><strong>普通列表上的列表理解</strong>-(每个循环489纳秒)</em></p>
<pre><code> mquadri$ python3 -m timeit -s "l1 = [1,2,6,8]; l2 = [2,3,5,8];" "[x for x in l1 if x not in l2]"
500000 loops, best of 5: 489 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/4211325/2063361">Daniel Pryden's</a><strong>生成器表达式,使用基于<code>set</code>的查找</strong></em>并键入强制转换到<code>list</code>-<em>(每个循环583 nsec)</em>:按照OP的请求显式键入强制转换到list,以获得最终对象为^{<cd5>。如果将<em>生成器表达式</em>替换为<em>列表理解</em>,则与<em>相同<a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a>基于<code>set</code>查找的列表理解。</em></p>
<pre><code> mquadri$ mquadri$ python3 -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "list(x for x in l1 if x not in l2)"
500000 loops, best of 5: 583 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>使用<code>filter()</code></strong></em>并显式键入转换到<code>list</code>(需要显式键入转换,如Python3.x中所示,它返回迭代器)<em>(每个循环681nsec)</em></p>
<pre><code> mquadri$ python3 -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "list(filter(lambda x: x not in l2, l1))"
500000 loops, best of 5: 681 nsec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/33491327/2063361">Akshay Hazari's</a><strong>使用<code>functools.reduce</code>+^{<cd11>>-(每个循环3.36 usec)</em>的组合:从Python 3.x显式键入casting到<code>list</code>,它开始返回迭代器。我们还需要导入<code>functools</code>以在Python 3.x中使用<code>reduce</code></p>
<pre><code> mquadri$ python3 -m timeit "from functools import reduce; l1 = [1,2,6,8]; l2 = [2,3,5,8];" "list(reduce(lambda x,y : filter(lambda z: z!=y,x) ,l1,l2))"
100000 loops, best of 5: 3.36 usec per loop
</code></pre>
</li>
</ol>
<h3><em>Python 2.7.16</em></h3>
<p>答案按性能顺序列出:</p>
<ol>
<li><p><em><a href="https://stackoverflow.com/a/4211239/2063361">Arkku's</a><strong><code>set</code>使用减法进行差分</strong>“-”运算-(每个循环0.0783 usec)</em></p>
<pre><code>mquadri$ python -m timeit -s "l1 = set([1,2,6,8]); l2 = set([2,3,5,8]);" "l1 - l2"
10000000 loops, best of 3: 0.0783 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>使用<code>set().difference()</code></strong>-(每个循环0.117 usec)</em></p>
<pre><code>mquadri$ mquadri$ python -m timeit -s "l1 = set([1,2,6,8]); l2 = set([2,3,5,8]);" "l1.difference(l2)"
10000000 loops, best of 3: 0.117 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>基于<code>set</code>查找的列表理解</strong>-(每个循环0.246 usec)</em></p>
<pre><code> mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "[x for x in l1 if x not in l2]"
1000000 loops, best of 3: 0.246 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/4211228/2063361">Donut's</a><strong>普通列表上的列表理解</strong>-(每个循环0.372 usec)</em></p>
<pre><code> mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = [2,3,5,8];" "[x for x in l1 if x not in l2]"
1000000 loops, best of 3: 0.372 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/65868213/2063361">Moinuddin Quadri's</a><strong>使用<code>filter()</code></strong>-(每个循环0.593usec)</em></p>
<pre><code> mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "filter(lambda x: x not in l2, l1)"
1000000 loops, best of 3: 0.593 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/4211325/2063361">Daniel Pryden's</a><strong>生成器表达式,使用基于<code>set</code>的查找</strong></em>并键入强制转换到<code>list</code>-<em>(每个循环0.964个)</em>:按照OP的请求显式键入强制转换到list以获得最终对象^{<cd5>。如果将<em>生成器表达式</em>替换为<em>列表理解</em>,则它将与<em>{a2}使用基于<code>set</code>的查找进行列表理解。</em></p>
<pre><code> mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "list(x for x in l1 if x not in l2)"
1000000 loops, best of 3: 0.964 usec per loop
</code></pre>
</li>
<li><p><em><a href="https://stackoverflow.com/a/33491327/2063361">Akshay Hazari's</a><strong>使用<code>functools.reduce</code>+<code>filter</code></strong>-(每个循环2.78 usec)</em></p>
<pre><code> mquadri$ python -m timeit "l1 = [1,2,6,8]; l2 = [2,3,5,8];" "reduce(lambda x,y : filter(lambda z: z!=y,x) ,l1,l2)"
100000 loops, best of 3: 2.78 usec per loop
</code></pre>
</li>
</ol>