Python - expanding the range between dot dot (id1..id2)
<p>Suppose i have a list of ids like:  </p>

<pre><code>ids= 1/1/n1..1/1/n5 , 1/1/x1 , 1/1/g1
</code></pre>

<p>Expected output: </p>

<pre><code>1/1/n1 , 1/1/n2 , 1/1/n3 , 1/1/n4 , 1/1/n5 ,1/1/x1, 1/1/g1
</code></pre>

<p>Means wherever i finds 'ids..ids', i will fill the gap </p>

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

<pre><code>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
</code></pre>

<p>Gives Expected Output:  </p>

<pre><code>['1/1/n1', '1/1/n2', '1/1/n3', '1/1/n4', '1/1/n5', '1/1/n6', '1/1/n7', '1/1/n8']
</code></pre>

<p>But how it can be solved easily , without complex logic ? </p>

<p>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:</p>

<pre><code>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
</code></pre>

<p>The last line is there merely to demonstrate the proper response.</p>

python list python-2.7