发电机功能
def group_by_heading( some_source ): buffer= [] for line in some_source: if line.startswith( "Heading" ): if buffer: yield buffer buffer= [ line ] else: buffer.append( line ) yield bufferwith open( "some_file", "r" ) as source: for heading_and_lines in group_by_heading( source ): heading= heading_and_lines[0] lines= heading_and_lines[1:] # process away.



