我可以在一个文件中存储迭代器,我可以从以后读取?这会减少空间消耗吗? [英] Can I store an iterator in a file which I can read from later? Will this reduce space consumption?

查看:278
本文介绍了我可以在一个文件中存储迭代器,我可以从以后读取?这会减少空间消耗吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个非常大的整数,大约为10 ** 200。现在将整数存储在文件中将占用一些空间。



如果我使用 yield ,我可以将迭代器存储在文件中吗?
这将保存任何资源吗?



迭代器可以生成如下:

  def rec():
for i in range(0,10 ** 200):
yield i

iterable = rec $ b


解决方案

建立在 larsmans 答案,可以构建一个自定义迭代器来执行此操作:

  class my_large_num(object):

def __init __(self):
self.num_iterations = 0

def __iter __(self):
return self


def next(self):
if self.num_iterations< 1:
self.num_iterations + = 1
return 10 ** 200
else:
raise StopIteration()

您可以:

  import pickle 
pickled_repr = pickle.dumps(my_large_num())
restored_object = pickle.loads(pickled_repr)
sum(restored_object)

这是因为下面的可迭代对象有一个 next()函数,它会引发 StopIteration 完成。我们所做的是创建一个实现这个功能的类。



在这个特定的情况下,无论你在类文件中存储了什么,以执行迭代,从而将 10 ** 200 存储在内存中,因此除了生成需要的数字之外,您不需要执行任何功能,您无需对对象进行序列化即可执行。 / p>

您可能在想 mmap 样式空间节省。这将内存映射到一个文件 - 但请注意,这仍然会影响程序的可用内存。


Let's say I have a very large integer, around the order of 10**200. Now storing the integer in a file will take some amount of space.

If I convert it into an iterator using yield, can I store the iterator in a file instead? Will this save any resources?

The iterator can be generated like this:

def rec():
  for i in range(0,10**200):
    yield i

iterable = rec()

解决方案

Building on larsmans answer, a custom iterator can be built to do this:

class my_large_num(object):

    def __init__(self):
        self.num_iterations = 0

    def __iter__(self):
        return self


    def next(self):
        if self.num_iterations < 1:
            self.num_iterations += 1
            return 10**200
        else:
            raise StopIteration()

You can then:

import pickle
pickled_repr = pickle.dumps(my_large_num())
restored_object = pickle.loads(pickled_repr)
sum(restored_object)

This works because underneath, iterable objects have a next() function which raises StopIteration when done. All we're doing is creating a class that implements this functionality.

In this specific case, regardless of the fact you have stored the class in a file, you still need to perform the iteration, and thus store 10**200 in memory, so you gain no functionality except generating the number on demand, which you can do without serializing the object.

You might be thinking of mmap style space saving. This maps memory to a file - note however this still affects the usable memory of your program.

这篇关于我可以在一个文件中存储迭代器,我可以从以后读取?这会减少空间消耗吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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