Python集与列表 [英] Python Sets vs Lists

查看:55
本文介绍了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集与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆