本文摘自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怎么安装turtle
什么是copy函数?直接赋值与copy的区别是什么?
Python的除法运算符是什么意思
Python中input函数的用法是什么?
Python如何转换时间戳
数据分析师为什么要学Python
Python中使用什么代替switch语句
Python实现管理站点的方法
如何用Python求素数
Python中数组,列表:冒号的用法介绍
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么对列表中元素去重复