Python 列表去重:使用 set() 函数高效移除重复元素
使用 Python 实现:列表去重,返回新列表内容:可以使用 set() 函数将列表转换成集合,因为集合中不允许存在重复元素,然后再将集合转换回列表即可。以下是一个示例代码:
def remove_duplicates(lst):
return list(set(lst))
# 测试代码
lst = [1, 2, 3, 3, 4, 5, 5, 6]
new_lst = remove_duplicates(lst)
print(new_lst)
输出结果为:[1, 2, 3, 4, 5, 6]。
原文地址: https://www.cveoy.top/t/topic/pAvg 著作权归作者所有。请勿转载和采集!