如何设置python列表/集的最大长度? [英] How to set a max length for a python list/set?

查看:905
本文介绍了如何设置python列表/集的最大长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c/c ++中,我们可以:

In c/c++, we could have:

maxnum = 10;
double xlist[maxnum];

如何为python列表/集设置最大长度?

How to set a maximum length for a python list/set?

推荐答案

您不需要,也不需要.

Python列表会根据需要动态增长和缩小,以适应其内容.集合被实现为哈希表,就像Python字典一样,其字典会根据需要动态增长和收缩以适应其内容.

Python lists grow and shrink dynamically as needed to fit their contents. Sets are implemented as a hash table, and like Python dictionaries grow and shrink dynamically as needed to fit their contents.

也许您正在寻找 collections.deque (需要maxlen参数)或使用 heapq (在达到最大值时使用heapq.heappushpop())代替的东西?

Perhaps you were looking for collections.deque (which takes a maxlen parameter) or something using a heapq (using heapq.heappushpop() when you have reached the maximum) instead?

这篇关于如何设置python列表/集的最大长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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