本文摘自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接口怎么写
pycharm怎么调背景颜色
怎么看Python安装了哪些库
django是什么
Python环境变量如何配置
Python file seek() 方法是什么?怎么理解并使用它?
Python的字符串与下标定义与使用方法(内有示例与解析)
Python编程语言是什么
Python中对数函数怎么表示
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么对列表中元素去重复