开发者

skip an element in zip

I have two files that are zipped with something like the following:

for line in zip(open(file1), open(file2)):
    # do-somethi开发者_如何学Cng

Unfortunately, now file2 has changed, and there is an additional line at the beginning. Yes, I could get rid of that manually (or with an additional script/program), but since the actual number of involved files is huge, I'd prefer to solve the problem at this level.

So, what I want is something like the following (which would be valid, if open(file) were subscriptable):

for line in zip(open(file1), open(file2)[1:]):
    # do-something


open gives you an iterator, so it's not "subscriptable" but it can easily be advanced by one (with the next builtin in 2.6 or better, the .next() method in older Python versions -- I'm assuming 2.6 or better here).

So where you'd like to say:

for line in zip(open(file1), open(file2)[1:]):

say, instead:

f2 = open(file2)
next(f2)
for line in zip(open(file1), f2):

or, if you're keen on one-liners:

import itertools as it
for line in it.izip(open(file1), it.islice(open(f2), 1, None)):

In the latter case, since I'm importing itertools anyway, I also take advantage to use its izip approach rather than the memory-wasting zip built-in;-).


Take a look at itertools:

for line in itertools.izip(
    open(file1),
    itertools.islice(open(file2), 1, None)
    ):
    # do something

Edit: changed from zip to the itertools.izip function.


f1 = open(file1)
f2 = open(file2)
f2.next()   # Skip the first line of file2 (used to be readline() - thanks, Alex)
for line in zip(f1, f2):
    # do-something
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜