Search code examples
pythonexceltext

How to convert a list of number ranges with hyphens and commas in single values


I have a list of numbers formatted like in the example below:

3074
88, 3074
1935, 3478-3480
3074, 3478-3479
27015-27030, 27036-27037
4380, 27000-27031, 27036

Basically, this is a collection of ports and port intervals. I would like to convert this list in a one dimensional array, like for instance:

3074
88
3074
1935
3478
3479
3480
...

Basically, I want a single column but with repeated values in order to retain the information about the frequency of the number showing in the list.

I preferably would like to do this in Excel or in Python.

Do you have any suggestions?

Thank you


Solution

  • A Python solution.

    data = """3074
    88, 3074
    1935, 3478-3480
    3074, 3478-3479
    27015-27030, 27036-27037
    4380, 27000-27031, 27036"""
    delimiters = "\n\r\t,"
    for d in delimiters:
        data = data.replace(d, ' ')
    
    lines = data.split(' ')
    numbers = []
    i = 0
    for line in lines:
        if line == '':
            continue
        elif '-' in line:
            t = line.split('-')
            numbers += range(int(t[0]), int(t[1]) + 1)
        else:
            numbers.append(int(line))
    

    Out: [3074, 88, 3074, 1935, 3478, 3479, 3480, 3074, 3478, 3479, 27015, 27016, 27017, 27018, 27019, 27020, 27021, 27022, 27023, 27024, 27025, 27026, 27027, 27028, 27029, 27030, 27036, 27037, 4380, 27000, 27001, 27002, 27003, 27004, 27005, 27006, 27007, 27008, 27009, 27010, 27011, 27012, 27013, 27014, 27015, 27016, 27017, 27018, 27019, 27020, 27021, 27022, 27023, 27024, 27025, 27026, 27027, 27028, 27029, 27030, 27031, 27036]