PHP前端开发

python删除list中的重复元素

百变鹏仔 1个月前 (01-22) #Python
文章标签 元素

可以使用append方法实现。

首先设一个临时列表保存结果,然后从头遍历原列表,如临时列表中没有当前元素则追加。

具体代码:

给定一个列表,要求删除列表中重复元素。

listA = ['python','语','言','是','一','门','动','态','语','言']
def deleteDuplicatedElementFromList2(list):        resultList = []        for item in list:                if not item in resultList:                        resultList.append(item)        return resultList

结果:

立即学习“Python免费学习笔记(深入)”;

#['python', '语', '言', '是', '一', '门', '动', '态']

推荐教程:python-tutorials.html" target="_blank">python教程