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 ?