Python效率:列表与元组 [英] Python efficiency: lists vs. tuples
问题描述
我有中等数量的基础对象.
I have a medium-amount of base objects.
这些基础对象将被放入集合中,并且这些集合将被杂乱地排列:已排序,截断等.
These base objects will be put in collections, and these collections will be munged around: sorted, truncated, etc.
不幸的是, n 足够大,以至于内存消耗有些令人担忧,并且速度越来越令人担忧.
Unfortunately, the n is large enough that memory consumption is slightly worrisome, and speed is getting concerning.
我的理解是,由于对元组进行了重复数据删除,因此它们的内存效率略高.
My understanding is that tuples are slightly more memory-efficient, since they are deduplicated.
无论如何,我想知道在Python 2.6/2.7中列表与元组的cpu/内存权衡是什么.
Anyway, I would like to know what the cpu/memory tradeoffs of lists vs. tuples are in Python 2.6/2.7.
推荐答案
如果您有一个元组和具有相同元素的列表,则该元组将占用较少的空间.由于元组是不可变的,因此您无法对其进行排序,添加等.我建议您观看
If you have a tuple and a list with the same elements, the tuple takes less space. Since tuples are immutable, you can't sort them, add to them, etc. I recommend watching this talk by Alex Gaynor for a quick intro on when to choose what datastructure in Python.
更新:再考虑一下,您可能希望研究优化对象的空间使用,例如,通过 namedtuple
实例代替实际对象.这可能会带来更大的节省,因为您有N个收藏夹,并且(大概)只有几个收藏夹出现在其中. namedtuple
特别棒.查看 Raymond Hettinger的讲话.
UPDATE: Thinking about it some more, you may want to look into optimizing the space usage of your objects, e.g., via __slots__
or using namedtuple
instances as proxies instead of the actual objects. This would likely lead to much bigger savings, since you have N of them and (presumbaly) only a few collections in which they appear. namedtuple
in particular is super awesome; check out Raymond Hettinger's talk.
这篇关于Python效率:列表与元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!