Python 集与列表

Python Sets vs Lists(Python 集与列表)
本文介绍了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 集与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)