我正在寻找一种编写生成器的好方法,该生成器从另一个列表/生成器/可迭代中获取项目流并对它们进行分组。
拆分物品很容易。例如,如果我们想要获取文件的行并将它们拆分为字符:
def lines2chars(filename):
with open(filename) as fh:
for line in fh: # Iterate over items
for char in line: # Split items up
yield char # Yield smaller items
将它们分组,例如产生段落,似乎很棘手。这就是我想出来的:
def lines2para(filename):
with fh as open(filename):
paragraph = [] # Start with an empty group
while True: # Infinite loop to be ended by exception
try:
line = next(fh) # Get a line
except StopIteration as e:
# If there isn't one...
# do whatever necessary
raise # and raise StopIteration for the caller
else:
paragraph.append(line) # Add to the group of items
if line == "\n": # If we've got a whole group
yield paragraph # yield it
paragraph = [] # and start a new group
在我看来,它并不漂亮。它正在使用迭代协议的内部,有一个无限循环,它已被打破,并且对我来说不太好。那么有没有人有更好的方法来编写这种类型的代码?
请记住,我正在寻找模式,而不是这个具体的例子。在我的情况下,我正在读取分组的数据,这些数据分组在数据包中,但每个级别与段落示例类似。
答案 0 :(得分:1)
import itertools as it
def lines2para(filename):
with open(filename) as fh:
for k, v in it.groupby(fh, key=lambda x: bool(x.strip())):
if k:
yield list(v)