I didn't quite know how to ask this question, or even search for the answer on Google, but I will write it out here. I have a sorted list of integers, that correspond to line numbers in a file. I would like to convert them into strings, but for the numbers that are sequential, I want the string to have the first number of the sequence, a dash, and then the last number. Here is an example:
line_nums = [ 1, 2, 3, 5, 7, 8, 9, 10 ]
I want to turn that list into:
[ '1-3', '5', '7', '8-10' ]
I wrote some code that works for the most part. On some sequences, it will put the same number in a string twice. On a recent execution of this code, the input was:
[ 10007, 10008, 10009, 10010, 10011, 10013, 10015, 10016, 10017, 10018, 10019 ]
But what I got back was:
[ '10007-10011', '10013-10013', '10015-10019' ]
Here is my code:
def get_line_numbers_concat(line_nums):
seq = []
final = []
last = 0
for index, val in enumerate(line_nums):
if last + 1 == val or index == 0:
seq.append(val)
last = val
else:
final.append(str(seq[0]) + '-' + str(seq[len(seq)-1]))
seq = []
seq.append(val)
last = val
if index == len(line_nums) - 1:
if len(seq) > 1:
final.append(str(seq[0]) + '-' + str(seq[len(seq)-1]))
else:
final.append(str(seq[0]))
final_str = ', '.join(map(str, final))
return final_str
The en dash is used to represent a span or range of numbers, dates, or time. There should be no space between the en dash and the adjacent material. Depending on the context, the en dash is read as “to” or “through.” The 2010–2011 season was our best yet.
The en dash is approximately the length of the letter n, and the em dash the length of the letter m. The shorter en dash (–) is used to mark ranges and with the meaning “to” in phrases like “Dover–Calais crossing.” The longer em dash (—) is used to separate extra information or mark a break in a sentence.
Hyphens are used to separate groups of numbers, such as in telephone numbers or numbers of financial accounts. But for almost all other cases, the correct punctuation mark is an en dash, which indicates a range or a difference. A span of years (such as “2009–2012”) or any other time range includes an en dash.
NUMBERS: from twenty-one to ninety-nine, when spelled out, are hyphenated. FRACTIONS: Hyphenate a fraction when it is used as a adjective (e.g., a two-thirds majority). Write as two words when used as a noun (e.g. two thirds of the participants).
You're almost there except in the case when seq[0]
is actually the same element as seq[len(seq)-1]
which you then simplify to the case of len(seq)==1
or as shown below if len(seq) > 1
then you perform your normal processing, otherwise JUST add the first element.
def get_line_numbers_concat(line_nums):
seq = []
final = []
last = 0
for index, val in enumerate(line_nums):
if last + 1 == val or index == 0:
seq.append(val)
last = val
else:
if len(seq) > 1:
final.append(str(seq[0]) + '-' + str(seq[len(seq)-1]))
else:
final.append(str(seq[0]))
seq = []
seq.append(val)
last = val
if index == len(line_nums) - 1:
if len(seq) > 1:
final.append(str(seq[0]) + '-' + str(seq[len(seq)-1]))
else:
final.append(str(seq[0]))
final_str = ', '.join(map(str, final))
return final_str
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With