python移除列表中的重复元素
2023-03-31 10:36:36 时间
所谓基础不牢,地动山摇,咱们基础学完了,但是要温故而知新。
涉及知识点
- python 遍历列表
- python 集合 set
- python 方法调用
代码解析
咱们通过遍历和集合两个方式来实现
首先导入使用的模块
import platform
# Python学习交流群:279199867 # 进群后可领取海量:Python视频教程、100本Python电子书、基础、爬虫、数据分析、web开发、机器学习、人工智能、面试题、Python学习路线图、问题解答!点击有道云笔记即可领取
画蛇添足一下
print("俱往矣,数风流人物,还看今朝")
移除列表中的重复元素
输入数据
input_list = [1, 2, 2, 3, 3, 3] print("输入数据: ", input_list)
方法1: 遍历列表
def method_1(): print("方法 1 : 遍历列表") result = [] for e in input_list: if e not in result: result.append(e) print("结果: ", result)
方法2: 使用集合 set
def method_2(): print("方法 2 : 使用集合 set") result = list(set(input_list)) print("结果: ", result)
全部代码
import platform print("俱往矣,数风流人物,还看今朝") input_list = [1, 2, 2, 3, 3, 3] print("输入数据: ", input_list) def method_1(): print("方法 1 : 遍历列表") result = [] for e in input_list: if e not in result: result.append(e) print("结果: ", result) def method_2(): print("方法 2 : 使用集合 set") result = list(set(input_list)) print("结果: ", result) method_1() method_2() print("Python 版本", platform.python_version())
运行结果
兄弟们快去试试吧!
来都来了,点个赞再走呗!
相关文章
- Python中的函数与方法 以及Bound Method和Unbound Method
- 一文贯通python文件读取
- Python 中的异步编程:Asyncio
- 7个你现在就该学习Python的理由
- 提高Python运行效率的六个窍门
- Python数据科学:神经网络
- 一篇文章看懂大数据分析就业前景及职能定位
- R和Python中的文本挖掘:8个入门小贴士
- 告诉你为什么Python有点慢,但我却无所谓?
- 专注学习DevOps编程语言Top 5推荐
- Python发送邮件脚本
- Python多进程并行编程实践: mpi4py 的使用
- Python语言在未来的发展前景
- Python vs Ruby: 谁是最好的 web 开发语言?
- Python对Ruby:谁在Web开发领域更胜一筹?
- Python一行代码完成并行任务
- Python开发者2017应该关注的七个类库
- python爬虫入门基本知识
- 在终端中优雅地编写Python
- Python机器学习实战:信用卡欺诈检测