Image

Imagejoxn wrote in Imagepython_dev

rockin' with itertools

I have four very large related ascii files. They contain four different components of an image, in (row, column) format. I want an iterator which returns a list:
[r, c, file0[r,c], file1[r,c], file2[r,c], file3[r,c]]
without loading all four files into memory.


from itertools import izip, imap

def ptiter(filenames):
    
    for r, row in enumerate(
                    imap(lambda l: izip(*[l[n] for n in range(len(filenames))]),
                        izip(*[imap(lambda l: imap(float, l.split()),
                                    file(filename))
                                for filename in filenames]))):
        for c, data in enumerate(row):
            yield [c, r] + [data[n] for n in range(len(filenames))]



Bonus -- works with any number of files.