在python中对列表中元素去重复有以下几种方法:
方法一:
用内置函数set:(推荐:python中set是什么意思)
list1 = [1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9] list2 = list(set(list1)) print(list2)
方法二:
遍历去除重复(推荐:在Python中遍历列表的方法有哪些)
立即学习“Python免费学习笔记(深入)”;
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)
列表推导式
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怎么对列表中元素去重复的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号