Python 集合与列表

编程入门 行业动态 更新时间:2024-10-07 18:23:06
本文介绍了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 集合与列表

本文发布于:2023-11-30 15:34:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!