I have some what big files and I'm trying to get all combinations with this code

for text1, text2 in itertools.product(open('text1.txt'), open('text2.txt')):
    t3 = (text1.strip() + text2.strip())
    time.sleep(1)
    print(t3)

testing with small files it worked fine but when using big files nothing happens I'm guessing its loading the file into memory anyway so it doesn't load the whole file into memory

推荐答案

This is documented:

Before product() runs, it completely consumes the input iterables, keeping pools of values in memory to generate the products. Accordingly, it is only useful with finite inputs.

Note, in this particular case, you may be able to do something like:

with open("text1.txt") as f1, open("text2.txt") as f2:
    for text1 in f1:
        for text2 in f2:
            # do some stuff
            t3 = (text1.strip() + text2.strip())
        f2.seek(0) # reset inner file cursor

This is possible due to the nature of file iterators - you can just seek to the beginning and the iterator is effectively reset (and this is nice and efficient too!). But this won't work with iterables or iterators in general, so itertools.product handles the general case by simply reifying two lists out fo the iterator

Python相关问答推荐

抓取rotowire MLB球员新闻并使用Python形成表格

根据二元组列表在pandas中创建新列

加速Python循环

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

名为__main__. py的Python模块在导入时不运行'

考虑到同一天和前2天的前2个数值,如何估算电力时间序列数据中的缺失值?

将scipy. sparse矩阵直接保存为常规txt文件

如何在PySide/Qt QColumbnView中删除列

如何排除prefecture_related中查询集为空的实例?

从列表中获取n个元素,其中list [i][0]== value''

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

如何在海上配对图中使某些标记周围的黑色边框

如何将一组组合框重置回无 Select tkinter?

如何重新组织我的Pandas DataFrame,使列名成为列值?

如何在信号的FFT中获得正确的频率幅值

如何使用大量常量优化代码?

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

Polars时间戳同步延迟计算

如何在表单中添加管理员风格的输入(PDF)

关于数字S种子序列内部工作原理的困惑