A question about finditer output in regularization

for index, row in train_set.iterrows ():

S = str(row[""])
M = re.finditer(match_target, S)
part_index_list = [(m.start(), m.start() + len(m.group()) - 1) for m in M]
part_list = [m.group() for m in M]
After

run, the part_index_list list has a normal value, and part_list has been empty all the time. Theoretically, part_index_list should be corresponding to part_list, empty or non-empty at the same time. The result seems to be unscientific.


because the elements in generator M are already consumed when assigning a value to part_index_list, there are no elements in M when assigning a value to part_list.


reason @ pein has been given.
what better way to do than to finditer again? , you can do this:

part_tuple = [((m.start(), m.start() + len(m.group()) - 1), m.group()) for m in M]
part_index_list, part_list = zip(*part_tuple)
Menu