简体   繁体   English

使用itertools的两个循环

[英]Using two loops of itertools

I am trying to run two loops of itertools but I am getting an error. 我正在尝试运行itertools的两个循环,但出现错误。 This is my code: 这是我的代码:

import itertools
with open('lognew.txt', 'r') as f:
    firstlines = itertools.islice(f, 0, None, 1)
    secondlines = itertools.islice(f, 0, None, 2)

    for (line1 in firstlines) and (line2 in secondlines):
        print line1 + line2 

I am getting this error : 我收到此错误:

File "C:\Users\abhi\Desktop\logedit.py", line 15
    for (line1 in firstlines) and (line2 in secondlines):
                            ^
SyntaxError: invalid syntax

I want the result as this: 我想要这样的结果:

<first line in firstlines> is meaning of  <first line in secondlines>
<second line in firstlines>  is meaning of <second line in secondlines>
<third line in firstlines>  is meaning of <third line in secondlines>

There is no error in reading files as the following code prints lines successfully. 读取文件没有错误,因为以下代码成功打印了行。

for line1 in firstlines :
    print line1
for line2 in secondlines :
    print line2

Any help is highly appreciated. 非常感谢您的帮助。

Your slices won't do what you think they'll do. 您的切片将无法执行您认为会执行的操作。 The first slice gives you just the first line, the second slice gives you the second next line , provided you iterate over it after the first slice. 第一个切片只给您第一行,第二个切片给您第二行 ,前提是您在第一个切片之后对其进行了迭代。 That's because the input file has already advanced one line, so the second slice object will skip one, give you the next, on iteration. 那是因为输入文件已经前进了一行,所以第二个切片对象将在迭代时跳过其中一个,为您提供下一个。 You'd line 1, then line 3, then line 4, then line 6, etc. if you had zipped the files. 如果压缩了文件,则需要第1行,第3行,第4行,第6行,依此类推。

What you really want, is pair-wise line looping. 您真正想要的是成对线路循环。 For that, you don't need to use slicing at all; 为此,您根本不需要使用切片。 use itertools.izip() ; 使用itertools.izip() ; it'll give you every two lines zipped together: 它将每两行压缩在一起:

from itertools import izip

with open('lognew.txt', 'r') as f:
    for line1, line2 in izip(f, f):
        print line1.rstrip('\n') + line2

Every iteration, izip() will get a value from the first argument (open file object f ), then from the second argument ( also open file object f ). 每次迭代时, izip()都会从第一个参数(打开文件对象f )获取一个值,然后从第二个参数( 打开文件对象f )获取一个值。 Because a file object advances to the next line every time it is iterated over, the above effectively gives you the lines from the file in pairs. 由于文件对象每次迭代都前进到下一行,因此以上内容有效地为您提供了文件中成对的行。

Note that if the file has an odd number of lines, you'll not see the last line. 请注意,如果文件的行数为奇数,则看不到最后一行。 If you need access to that last line, use itertools.izip_longest() instead: 如果需要访问最后一行,请改用itertools.izip_longest()

from itertools import izip_longest

with open('lognew.txt', 'r') as f:
    for line1, line2 in izip_longest(f, f, fillvalue='\n'):
        print line1.rstrip('\n') + line2

which will add an empty line ( fillvalue='\\n' ) if the number of lines is odd. 如果行数为奇数,它将添加一个空行( fillvalue='\\n' )。

In Python 3, itertools.izip() has replaced the built-in zip() function, and itertools.izip_longest() has been renamed to itertools.zip_longest() . 在Python 3中, itertools.izip()取代了内置的zip()函数,并且itertools.izip_longest()已重命名为itertools.zip_longest()

for line1, line2 in itertools.izip(f1, f2):

Python双迭代

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM