Python Sets vs Lists(Python 集与列表)
问题描述
在 Python 中,哪种数据结构更高效/更快?假设顺序对我来说并不重要,并且无论如何我都会检查重复项,那么 Python 集是否比 Python 列表慢?
In Python, which data structure is more efficient/speedy? Assuming that order is not important to me and I would be checking for duplicates anyway, is a Python set slower than a Python list?
推荐答案
这取决于你打算用它做什么.
It depends on what you are intending to do with it.
在确定对象是否存在于集合中时,集合明显更快(如 x in s),但在迭代其内容时比列表慢.
Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s), but are slower than lists when it comes to iterating over their contents.
您可以使用 timeit 模块来查看哪种方式更适合您的情况.
You can use the timeit module to see which is faster for your situation.
这篇关于Python 集与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 集与列表
基础教程推荐
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
