I have a list of line numbers in a text file.
linenrs = [8,12,18,21]
I want to join all lines from one line number to the next line number.
This is what I've created and works.
newlist, templist = [], []
counter = 0
separator = " "
for i in range(1, 24): #23 lines in textfileline
temps = texfileline(i)
if i in linenrs:
if counter == 0:
templist.append(temps)
counter = 1
elif counter == 1:
newlist.append(separator.join(templist))
templist = []
templist.append(temps)
counter = 1
elif counter == 1 and i <= linenrs[-1]:
templist.append(temps)
else:
newlist.append(temps)
Is this possible with list manipulation?
I would like to obtain such a list (and then join the text of these lines in the textfile):
newlist = [[8,9,10,11], [12,13,14,15,16,17], [18,19,20], [21,22,23]]
CodePudding user response:
linenrs = [8,12,18,21]
last_line_number = 24
line_number_until = [number for number in linenrs[1:]] [last_line_number]
newlist = [list(range(start, end) for start, end in zip(linenrs, line_number_until)]
CodePudding user response:
Using itertools.islice with specific ranges is a good choice for such task.
Suppose we have an input file with structure <consecutive number><text>
:
1 text
2 text
3 text
...
27 text
28 text
29 text
30 text
from itertools import islice, zip_longest
linenrs = [8,12,18,21]
ranges = zip_longest(linenrs, linenrs[1:])
with open('test.txt') as f:
list(islice(f, linenrs[0] - 1)) # skip starting chunk
lines_slices = []
for from_, to_ in ranges:
lines_slices.append(list(islice(f, 0, 1 if to_ is None else to_ - from_)))
print(lines_slices)
The output (sublists of lines):
[['8 text\n', '9 text\n', '10 text\n', '11 text\n'],
['12 text\n', '13 text\n', '14 text\n', '15 text\n', '16 text\n', '17 text\n'],
['18 text\n', '19 text\n', '20 text\n'],
['21 text\n']]