Basically, I want a fancy oneliner that doesn't read all of the files I'm looking at into memory, but still processes them all, and saves a nice sample of them.
The oneliner I would like to do is:
return [bar(line) for line in findex] # but skip every nth term
enumerate to get the index, and a filter using modulo to take every other line:
return [bar(line) for i,line in enumerate(findex) if i%2]
Generalize that with
i%n so everytime that the index is divisible by
bar(line) isn't issued into the listcomp.
Edit: if you're sure that
findex is subscriptable (
tuple) you could use
range with step which would avoid to iterate on all occurrences (if
n is big the
enumerate solution is a CPU waster). So like posted in a now deleted answer:
[bar(findex[i]) for i in range (0, len(findex), 2) ]
enumerate works for every iterable (file handle, generator ...).
EDIT: if you want to still compute all the values (because you need the side effect generated by
bar), the above doesn't work because the filter prevents that, so you have to do that in 2 passes, for instance using
map to apply your function to all items of
findex and pick only the results you're interested in (but it guarantees that all of the lines are processed):
l = [x for i,x in enumerate(map(bar,findex)) if i%n]