简体   繁体   English

在Python中预分配非常大的数组会导致MemoryError

[英]Preallocate very large array in Python leads to MemoryError

I am trying to preallocate a list in python 我正在尝试在python中预分配列表

c=[1]*mM #preallocate array

My Problem is that I run in to a MemoryError since 我的问题是,由于

mM=4999999950000000

What is the best way to deal with this. 处理此问题的最佳方法是什么。 I am thinking about creating a new object where is split my list at about a value of 500000000 . 我正在考虑创建一个新对象,将我的列表拆分成大约500000000的值。 Is this what I should do or is there a best practice to create an array with a lot of inputs? 这是我应该做的还是创建具有大量输入的数组的最佳实践?

Using a Generator 使用发电机

You are attempting to create an object that you very likely will not be able to fit into your computer's memory. 您正在尝试创建一个很可能无法放入计算机内存的对象。 If you truly need to represent a list of that length, you can use a generator that dynamically produces values as they are needed. 如果确实需要表示该长度的列表,则可以使用生成器,该生成器根据需要动态生成值。

def ones_generator(length):
    for _ in range(length):
        yield 1

gen = ones_generator(4999999950000000)
for i in gen:
    print(i)  # prints 1, a lot

Note: The question is tagged for Python 3, but if you are using Python 2.7, you will want to use xrange instead of range . 注意:这个问题是针对Python 3标记的,但是如果您使用的是Python 2.7,则需要使用xrange而不是range

Using a Dictionary 使用字典

By the sound of your question, you do not actually need to preallocate a list of that length, but you want to store values very sparsely at indexes that are very large. 根据您的问题,您实际上不需要预分配该长度的列表,但是您希望将稀疏的值存储在非常大的索引中。 This pattern matches the dict type in Python more so than the list . 这种模式比Python中的dict类型更符合list You can simply store values in a dictionary, without pre-allocating they keys/space, Python handles that under the hood for you. 您可以简单地将值存储在字典中,而无需预先分配键/空间,Python会为您处理这些信息。

dct = {}
dct[100000] = "A string"
dct[592091] = 123
dct[4999999950000000] = "I promise, I need to be at this index"

print(dct[4999999950000000])
# I promise, I need to be at this index

In that example, I just stored str and int values, but they can be any object in Python. 在该示例中,我只存储了strint值,但它们可以是Python中的任何对象。 The best part about this is that this dictionary will not consume memory based on the maximum index (like a list would) but instead based on how many values are stored within it. 最好的部分是,该字典不会根据最大索引(如list )消耗内存,而是根据其中存储了多少值来消耗内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM