java – 算法 – 如何有效地删除列表中的重复元素?
发布时间:2020-12-14 16:34:04 所属栏目:Java 来源:网络整理
导读:有一个列表L.它包含任意类型的元素. 如何有效地删除此列表中的所有重复元素?订单必须保留 只需要一个算法,所以不允许导入任何外部库. 相关问题 In Python,what is the fastest algorithm for removing duplicates from a list so that all elements are uni
有一个列表L.它包含任意类型的元素.
如何有效地删除此列表中的所有重复元素?订单必须保留 只需要一个算法,所以不允许导入任何外部库. 相关问题 > In Python,what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order? 解决方法
假设订单事宜:
>创建一个空集S和一个空列表M. 在Python中: >>> L = [2,1,4,3,5,2,6,5] >>> S = set() >>> M = [] >>> for e in L: ... if e in S: ... continue ... S.add(e) ... M.append(e) ... >>> M [2,6] 如果订单不重要: M = list(set(L)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |