本文摘自php中文网,作者尚,侵删。

在Python中对列表中元素去重复有以下几种方法:
方法一:
用内置函数set:(推荐:python中set是什么意思)
1 2 3 | list1 = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9]
list2 = list(set(list1))
print (list2)
|
方法二:
遍历去除重复(推荐:在Python中遍历列表的方法有哪些)
1 2 3 4 5 6 | list1 = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9]
list2=[]
for i in list1:
if not i in list2:
list2.append(i)
print (list2)
|
列表推导式
1 2 3 | list1 = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9]
list2=[]
[list2.append(i) for i in list1 if not i in list2]
|
更多Python相关技术文章,请访问Python教程栏目进行学习!
以上就是python怎么对列表中元素去重复的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
给小白整理的第一篇Python知识点
Python下载好了怎么打开
Python怎么读出当前时间精度到秒
Python输出结果怎么换行
Python队列的定义与使用方法实例详解
Python绝对值怎么打
Python 编程开发 实用经验和技巧大放送
Python文件名的批量修改
Python如何安装包
Python中如何导入math库
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么对列表中元素去重复