numpy数组和python列表是否经过优化以动态增长? [英] Is numpy array and python list optimized to be dynamically growing?

查看:173
本文介绍了numpy数组和python列表是否经过优化以动态增长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着时间的流逝,我已经使用list.append()函数以及numpy数组的numpy.append()函数完成了许多需要我做的事情.我注意到,当数组的大小很大时,的增长速度都非常慢.

I have done over the time many things that require me using the list's .append() function, and also numpy.append() function for numpy arrays. I noticed that both grow really slow when sizes of the arrays are big.

我需要一个动态增长的数组,其大小约为100万个元素.我可以自己实现,就像 std::vector 用C ++制作一样,通过添加无法从外部访问的缓冲区长度(保留长度).但是我必须重新发明轮子吗?我想应该在某个地方实现它.所以我的问题是:Python中已经存在这样的东西吗?

I need an array that is dynamically growing for sizes of about 1 million elements. I can implement this myself, just like std::vector is made in C++, by adding buffer length (reserve length) that is not accessible from the outside. But do I have to reinvent the wheel? I imagine it should be implemented somewhere. So my question is: Does such a thing exist already in Python?

我的意思:Python中是否存在一种能够在大多数时间以O(C)的时间复杂度动态增长的数组类型?

What I mean: Is there in Python an array type that is capable of dynamically growing with time complexity of O(C) most of the time?

推荐答案

两者都使用基础数组.相反,您可以使用collections.deque,它专门用于在两端添加和删除元素,复杂度为O(1)

Both use an underlying array. Instead, you can use collections.deque which is made for specifically adding and removing elements at both ends with O(1) complexity

这篇关于numpy数组和python列表是否经过优化以动态增长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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