如何将迭代器返回到Python中的前一行
我一次处理两行。为此,我有一个使用枚举的for循环:如何将迭代器返回到Python中的前一行,python,python-3.x,io,Python,Python 3.x,Io,我一次处理两行。为此,我有一个使用枚举的for循环: for id, line in enumerate(file): if id % 2 == 0: line1 = line print("First line: ", line1) else: line2 = line print("Second line: ", line2) print("Both lines: ", line1 + line2
for id, line in enumerate(file):
if id % 2 == 0:
line1 = line
print("First line: ", line1)
else:
line2 = line
print("Second line: ", line2)
print("Both lines: ", line1 + line2)
对于此输入,一切都正常工作:
This is line 1
This is line 2
输出为:
First line: This is line 1
Second line: This is line 2
Both lines: This is line1 This is line2
问题:我不想处理以开头的行。
。我该怎么做?我可以简单地添加一个条件,例如if line.startswith(“\”)
,但是现在迭代器将无论如何递增,因此如果我有这样的输入:
Line1
_:Anthing
Line2
:\u任何东西都将被视为第2行,只需检查它是否以”开头,但由于它以”开头,我不想处理它,我仍然希望第二行是第2行。因此,在检查行之后,如果它以一个特殊字符开头,如何将迭代器返回到没有特殊字符的最后一行
在这种情况下,我希望输出为:
First Line: Line1
_:Anything
Second Line: Line2
Both lines: Line1 Line2
您没有“迭代器”-您只记住一行:
t = """Line1
_:Anthing1
Line2
Line3
_:Anthing2
Line4"""
line1 = None # these are not iterators, they are just lines
line2 = None # same here, just one memorized line
# the index you get is not needed at all ...
for line in t.split("\n"): # instead of "for line in file:"
# base case
if line.startswith("_:"):
print(line) # no processing
elif line1 is None and line2 is None: # remember first line
line1 = line
print("First line: ", line1)
elif line1 is not None: # we got a first line, so this is the second one
line2 = line
print("Second line: ", line2)
print("Both lines: ", line1 + line2)
# do processing on line1 and line2
# reset memorized lines
line1 = None
line2 = None
输出:
First line: Line1
_:Anthing1
Second line: Line2
Both lines: Line1Line2
First line: Line3
_:Anthing2
Second line: Line4
Both lines: Line3Line4
您没有“迭代器”-您只记住一行:
t = """Line1
_:Anthing1
Line2
Line3
_:Anthing2
Line4"""
line1 = None # these are not iterators, they are just lines
line2 = None # same here, just one memorized line
# the index you get is not needed at all ...
for line in t.split("\n"): # instead of "for line in file:"
# base case
if line.startswith("_:"):
print(line) # no processing
elif line1 is None and line2 is None: # remember first line
line1 = line
print("First line: ", line1)
elif line1 is not None: # we got a first line, so this is the second one
line2 = line
print("Second line: ", line2)
print("Both lines: ", line1 + line2)
# do processing on line1 and line2
# reset memorized lines
line1 = None
line2 = None
输出:
First line: Line1
_:Anthing1
Second line: Line2
Both lines: Line1Line2
First line: Line3
_:Anthing2
Second line: Line4
Both lines: Line3Line4
看起来您希望查看文件的每一行,但行是偶数还是奇数取决于它的值以及它在文件中的位置。我们只想在检查它的前缀后进行这样的赋值,可能是用类似
parity = itertools.cycle([True, False]) # even, odd, even, odd, ...
for line in file:
if line.startswith("_:"):
print(line)
continue
if next(parity):
line1 = line
print("First line: {}".format(line1))
else:
line2 = line
print("Second line: {}".format(line2))
print("Both line: {}{}".format(line1, line2))
无限、交替的布尔级数奇偶校验
替换从枚举
获得的行号。它允许您等到决定使用行
后再“询问”这是偶数行还是奇数行
另一种方法是,您的原始代码同时迭代奇偶校验
和文件
,而不是单独推进奇偶校验
。您可以将原始代码重写为
for is_even, line in zip(cycle([True, False], file):
if is_even:
line1 = line
print("First line: ", line1)
else:
line2 = line
print("Second line: ", line2)
print("Both lines: ", line1 + line2)
因此请注意,我的贡献只是将对next(parity)
的隐式调用替换为显式的条件调用。您似乎希望查看文件的每一行,但行是偶数还是奇数取决于它的值及其在文件中的位置。我们只想在检查它的前缀后进行这样的赋值,可能是用类似
parity = itertools.cycle([True, False]) # even, odd, even, odd, ...
for line in file:
if line.startswith("_:"):
print(line)
continue
if next(parity):
line1 = line
print("First line: {}".format(line1))
else:
line2 = line
print("Second line: {}".format(line2))
print("Both line: {}{}".format(line1, line2))
无限、交替的布尔级数奇偶校验
替换从枚举
获得的行号。它允许您等到决定使用行
后再“询问”这是偶数行还是奇数行
另一种方法是,您的原始代码同时迭代奇偶校验
和文件
,而不是单独推进奇偶校验
。您可以将原始代码重写为
for is_even, line in zip(cycle([True, False], file):
if is_even:
line1 = line
print("First line: ", line1)
else:
line2 = line
print("Second line: ", line2)
print("Both lines: ", line1 + line2)
请注意,我的贡献只是将对next(parity)
的隐式调用替换为显式、有条件的调用。我不想删除它们,我想将所有内容打印到输出中,但要打印没有特殊字符的行,我想给它们添加一些东西。只要保留一个单独的计数器变量,每当行以.
@pault开头时递增,那么大多数文件内容都必须加载到内存中。enumerate
只需要一个迭代器;它不必是一个具体的清单<代码>枚举(如果不是l.startswith(“-”),则文件中l代表l)
。请查看我想要的输出。我不想摆脱这些台词。我仍然希望将它们打印在正确的位置,但不进行处理。我不希望删除它们,我希望打印输出的所有内容,但打印没有特殊字符的行,我想给它们添加一些东西。只要保留一个单独的计数器变量,每当行以.
@pault开头时递增,那么大多数文件内容都必须加载到内存中。enumerate
只需要一个迭代器;它不必是一个具体的清单<代码>枚举(如果不是l.startswith(“-”),则文件中l代表l)
。请查看我想要的输出。我不想摆脱这些台词。我仍然想将它们打印在正确的位置,但不进行处理。非常感谢,但它给了我以下错误:用于文件中的行。拆分(“\n”):\AttributeError:“\u io.TextIOWrapper”对象没有属性“split”
t.split(\n”)
在这个答案中只是文件
的替身,若要提供一系列字符串以进行迭代。@Lucas您可以使用来表示uu,枚举(文件)中的行:
并忽略索引-或者使用来表示文件中的行:
-您可能需要在结束时禁用任何'\n'
,尽管在后处理行时-请随意使用。非常感谢,但它给了我一个错误:代表file.split(“\n”):\AttributeError:“\u io.TextIOWrapper”对象没有属性“split”
t.split(\n”)
只是这个答案中文件
的一个替身,用来提供一系列字符串进行迭代。@Lucas您可以使用代表枚举(文件)中的行:
,只需忽略索引-或对文件中的行使用:
-在对行进行后处理时,您可能需要在结尾处禁用任何'\n'
。我喜欢这个循环:)非常感谢!!非常感谢+1但是循环切换布尔值似乎有些过火,“如果奇偶校验:奇偶校验=不奇偶校验”等有什么错吗?我喜欢流和不可变状态(如果可能的话)。我喜欢循环:)非常感谢!!非常感谢+1但循环切换布尔值似乎有些过火,“如果奇偶校验:奇偶校验=非奇偶校验”等有什么不对?我喜欢流和不可变状态(如果可能的话)。