本文摘自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的调试;print()和断言(实例解析二)
java和Python先学哪个
Python中subprocess库的用法介绍
Python开发tornado网站之requesthandler:输入捕捉
Python shell怎么打开
现在学Python能做什么?学完Python可以当黑客吗
Python的format怎么用
Python学习法则
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么对列表中元素去重复