0

Suppose i have a list of ids like:

ids= 1/1/n1..1/1/n5 , 1/1/x1 , 1/1/g1

Expected output:

1/1/n1 , 1/1/n2 , 1/1/n3 , 1/1/n4 , 1/1/n5 ,1/1/x1, 1/1/g1

Means wherever i finds 'ids..ids', i will fill the gap

I have written a very basic code, but i am looking for more pythonic solution

import re
ports_list=['1/1/n1..1/1/n8']

n = 2
port_range=[]
for port in ports_list:
    if '..' in port:
        groups = port.split('..')     #gives  ['1/1/n1', '1/1/n8']
        for item in groups:
            port_split = item.split('/')
            port_join='/'.join(port_split[:n]), '/'.join(port_split[n:])
            port_join=port_join[0]+"/"
            port_split=port_split[2]    # n1     n8
            get_str=port_split[0][0]
            num=re.findall(r'\d+', port_split) # 1 8
            port_range.append(num[0])
        #remove port with '..
        ports_list.remove(port)

n1=port_range[0]
n2=port_range[1]
final_number_list=list(range(int(n1),int(n2)+1))
my_new_list = [ get_str + str(n) for n in final_number_list]
final_list=[ port_join + str(n) for n in my_new_list]
ports_list=ports_list+final_list
print ports_list

Gives Expected Output:

['1/1/n1', '1/1/n2', '1/1/n3', '1/1/n4', '1/1/n5', '1/1/n6', '1/1/n7', '1/1/n8']

But how it can be solved easily , without complex logic ?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Dave
  • 221
  • 2
  • 12

2 Answers2

2

Not sure it is more readable or better than your current approach, but we can use regular expressions to extract the common part and the range borders from a string:

import re


def expand(l):
    result = []
    pattern = re.compile(r"^(.*?)(\d+)$")
    for item in l:
        # determine if it is a "range" item or not
        is_range = '..' in item
        if not is_range:
            result.append(item)
            continue

        # get the borders and the common reusable part
        borders = [pattern.match(border).groups() for border in item.split('..')]
        (common_part, start), (_, end) = borders

        for x in range(int(start), int(end) + 1):
            result.append("%s%d" % (common_part, x))

    return result

print(expand(['1/1/n1..1/1/n8']))
print(expand(['1/1/n1..1/1/n5', '1/1/x1', '1/1/g1']))

Prints:

['1/1/n1', '1/1/n2', '1/1/n3', '1/1/n4', '1/1/n5', '1/1/n6', '1/1/n7', '1/1/n8']
['1/1/n1', '1/1/n2', '1/1/n3', '1/1/n4', '1/1/n5', '1/1/x1', '1/1/g1']

where in ^(.*?)(\d+)$:

  • ^ matches the beginning of a string (not actually needed in our case since we use .match() - it will start searching from the beginning of a string anyway - leaving it there just because "Explicit is better than implicit")
  • (.*?) is a capturing group that saves any characters any number of times in a non-greedy fashion
  • (\d+) is a capturing group that would save one or more consecutive digits
  • $ matches the end of a string
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Here's the base code; I'll let you recombine it as desired, including list comprehensions that you already have. Just split the range as you're already doing. Instead of de-constructing the entire string, just take that last digit and run the range:

ports_list=['1/1/n1..1/1/n8']

for port in ports_list:
    if ".." in port:
        left, right = port.split("..")
        for i in range(int(left[-1]), int(right[-1])+1):
            new_port = left[:-1] + str(i)
            print new_port

The last line is there merely to demonstrate the proper response.

Prune
  • 76,765
  • 14
  • 60
  • 81