Python合并列表并去重
有勇气的牛排
562
Python
2024-09-22 21:18:37
1 兩個簡單的案例
A = [1, 3, 2]
B = [3, 4, 5]
C = A + B
print(set(C))
print(list(set(C)))
print(dict.fromkeys(C))
print(list(dict.fromkeys(C)))

2 萬物節可util
https://www.couragesteak.com/article/481
class UtilList:
@staticmethod
def merge_list_order(*lists):
"""
fromkeys 在 Python 3.7 及以上版本中,字典保持插入顺序
"""
return list(dict.fromkeys(item for lst in lists for item in lst))
@staticmethod
def merge_list_unorder(*lists):
"""
set 使用 哈希表来存储元素
:param lists:
:return:
"""
return list(set(item for lst in lists for item in lst))
A = [1, 3, 2]
B = [3, 4, 5]
res_order = UtilList.merge_list_order(A, B)
print("保持原顺序 去重:", res_order)
res_un_order = UtilList.merge_list_unorder(A, B)
print("不考虑顺序 去重:", res_un_order)
<h2><a id="1__0"></a>1 兩個簡單的案例</h2>
<pre><div class="hljs"><code class="lang-python">A = [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>]
B = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
C = A + B
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">set</span>(C))
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">set</span>(C)))
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">dict</span>.fromkeys(C))
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">dict</span>.fromkeys(C)))
</code></div></pre>
<p><img src="https://static.couragesteak.com/article/2c1a7ae6d3c9ae205992a5b800b10129.png" alt="Python合并列表并去重" /></p>
<h2><a id="2_util_18"></a>2 萬物節可util</h2>
<p><a href="https://www.couragesteak.com/article/481" target="_blank">https://www.couragesteak.com/article/481</a></p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">UtilList</span>:
<span class="hljs-comment"># 合并多个列表 保持原顺序 去重</span>
<span class="hljs-meta"> @staticmethod</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">merge_list_order</span>(<span class="hljs-params">*lists</span>):
<span class="hljs-string">"""
fromkeys 在 Python 3.7 及以上版本中,字典保持插入顺序
"""</span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">list</span>(<span class="hljs-built_in">dict</span>.fromkeys(item <span class="hljs-keyword">for</span> lst <span class="hljs-keyword">in</span> lists <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> lst))
<span class="hljs-comment"># 合并多个列表 去重 不考虑顺序</span>
<span class="hljs-meta"> @staticmethod</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">merge_list_unorder</span>(<span class="hljs-params">*lists</span>):
<span class="hljs-string">"""
set 使用 哈希表来存储元素
:param lists:
:return:
"""</span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">list</span>(<span class="hljs-built_in">set</span>(item <span class="hljs-keyword">for</span> lst <span class="hljs-keyword">in</span> lists <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> lst))
A = [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>]
B = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
<span class="hljs-comment"># 保持原顺序去重</span>
res_order = UtilList.merge_list_order(A, B)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"保持原顺序 去重:"</span>, res_order)
<span class="hljs-comment"># 输出: [1, 3, 2, 4, 5]</span>
<span class="hljs-comment"># 不考虑顺序去重</span>
res_un_order = UtilList.merge_list_unorder(A, B)
<span class="hljs-built_in">print</span>(<span class="hljs-string">"不考虑顺序 去重:"</span>, res_un_order)
<span class="hljs-comment"># 输出: [1, 2, 3, 4, 5]</span>
</code></div></pre>
留言