Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write ranges of numbers with dashes

Tags:

python

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
like image 632
rgutierrez1014 Avatar asked Apr 02 '15 17:04

rgutierrez1014


People also ask

How do you hyphenate a range of numbers?

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.

Which dash is used for ranges?

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.

What is a dash in numbers?

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.

Are numbers written with dashes?

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).


1 Answers

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
like image 198
nvuono Avatar answered Sep 20 '22 22:09

nvuono