Depending on how your use case uses a string, a faster way to delete it may not be deleted.
If you plan to sequentially access the lines in a line, you can create a generator that skips the first and last lines, and each line will be consumed, rather than completely creating a new set of copies of all the lines.
A subscriber way to avoid the first and last line is to iterate over the line without making unnecessary copies, tracking the next three lines and returning only the second, so the iteration will be completed before reaching the last line, without requiring to know the position of the last line break.
The following function should give you the desired result:
def split_generator(s): # Keep track of start/end positions for three lines start_prev = end_prev = 0 start = end = 0 start_next = end_next = 0 nr_lines = 0 for idx, c in enumerate(s): if c == '\n': nr_lines += 1 start_prev = start end_prev = end start = start_next end = end_next start_next = end_next end_next = idx if nr_lines >= 3: yield s[(start + 1) : end] # Handle the case when input string does not finish on "\n" if s[-1] != '\n' and nr_lines >= 2: yield s[(start_next+1):end_next]
You cannot verify this:
print("1st example") for filtered_strs in split_generator('first\nsecond\nthird'): print(filtered_strs) print("2nd example") for filtered_strs in split_generator('first\nsecond\nthird\n'): print(filtered_strs) print("3rd example") for filtered_strs in split_generator('first\nsecond\nthird\nfourth'): print(filtered_strs) print("4th example") for filtered_strs in split_generator('first\nsecond\nthird\nfourth\n'): print(filtered_strs) print("5th example") for filtered_strs in split_generator('first\nsecond\nthird\nfourth\nfifth'): print(filtered_strs)
Will generate output:
1st example second 2nd example second 3rd example second third 4th example second third 5th example second third fourth
Please note that the biggest advantage of this approach is that only one new line will be created at this time and there will be practically no time to create the first line of output (instead of waiting until all lines are found before continuing) but, again it may or may not be useful depending on your use case.
papirrin
source share