<p><code></p> <h2 id="articleHeader0">"<a href="http://www.js-code.com/tag/%e6%95%b0%e7%bb%84" title="数组" target="_blank">数组</a>去重" 是实际应用中常用的操作,出现在面试题中的概率也很高,今天简述一下 Python 和 JavaScript 中<a href="http://www.js-code.com/tag/%e6%95%b0%e7%bb%84" title="数组" target="_blank">数组</a>去重的方法</h2> <h3 id="articleHeader1"><a href="http://www.js-code.com/tag/python" title="python" target="_blank">python</a><a href="http://www.js-code.com/tag/%e6%95%b0%e7%bb%84" title="浏览关于“数组”的文章" target="_blank" class="tag_link">数组</a>去重</h3> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text=">>> a = [9,8,7,9,7,1,2,1,2,5,3]<br /> >>> new_a = list(set(a))<br /> >>> new_a<br /> [1, 2, 3, 5, 7, 8, 9]<br /> #此时new_a未保持原有的顺序,对new_a进行排序<br /> >>> new_a.sort(key = a.index)<br /> >>> new_a<br /> [9, 8, 7, 1, 2, 5, 3]<br /> " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs ruby"><code><span class="hljs-meta">&gt;&gt;</span>&gt; a = [<span class="hljs-number">9</span>,<span class="hljs-number">8</span>,<span class="hljs-number">7</span>,<span class="hljs-number">9</span>,<span class="hljs-number">7</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">3</span>] <span class="hljs-meta">&gt;&gt;</span>&gt; new_a = list(set(a)) <span class="hljs-meta">&gt;&gt;</span>&gt; new_a [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>] <span class="hljs-comment">#此时new_a未保持原有的顺序,对new_a进行排序</span> <span class="hljs-meta">&gt;&gt;</span>&gt; new_a.sort(key = a.index) <span class="hljs-meta">&gt;&gt;</span>&gt; new_a [<span class="hljs-number">9</span>, <span class="hljs-number">8</span>, <span class="hljs-number">7</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">3</span>] </code></pre> <h3 id="articleHeader2">JavaScript数组去重</h3> <div class="widget-codetool" style="display:none;"> <div class="widget-codetool--inner"> <span class="selectCode code-tool" data-toggle="tooltip" data-placement="top" title="" data-original-title="全选"></span><br /> <span type="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="<!DOCTYPE html><br /> <html lang=&quot;en&quot;><br /> <head><br /> <meta charset=&quot;UTF-8&quot;><br /> <title>Document</title><br /> <script type=&quot;text/javascript&quot;> var array1 = [1,2,6,8,4,2,3,9,47,1,2,23,5,8,3]; var result = []; for (var i = 0; i < array1.length; i++) { if (array1.indexOf(array1[i])==i) //如过该元素在数组中第一次出现的位置 == 该元素当前的位置【A】 { result.push(array1[i]);//将符合【A】条件的元素加入到result中 } } alert(result); </script><br /> </head><br /> <body></p> <p></body><br /> </html><br /> " title="" data-original-title="复制"></span> </div> </p></div> <pre class="hljs xml"><code><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="actionscript"> <span class="hljs-keyword">var</span> array1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>,<span class="hljs-number">4</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">9</span>,<span class="hljs-number">47</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">23</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>,<span class="hljs-number">3</span>]; <span class="hljs-keyword">var</span> result = []; <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; array1.length; i++) { <span class="hljs-keyword">if</span> (array1.indexOf(array1[i])==i) <span class="hljs-comment">//如过该元素在数组中第一次出现的位置 == 该元素当前的位置【A】</span> { result.push(array1[i]);<span class="hljs-comment">//将符合【A】条件的元素加入到result中</span> } } alert(result); </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span> </code></pre> <p><span class="img-wrap"><img data-src="/img/bV3YaY?w=413&amp;h=135" src="https://static.segmentfault.com/v-5cc2cd8e/global/img/squares.svg" alt="运行结果" title="运行结果" style="cursor: pointer;"></span></p> <p></code></p>

本文固定链接: http://www.js-code.com/python/python_50511.html