I have a list that has elements in this form, the lines may change, but the formats remain similar:
["Radio0","Tether0","Serial0/0","Eth0/0","Eth0/1","Eth1/0","Eth1/1","vlanX","modem0","modem1","modem2","modem3","modem6"]
I would like to convert it to the list below. You can see that it will remove copies of the same occurrence of a string, such as Eth - with only one occurrence in a new list and convert the numbers in x and y to a more general one:
["RadioX","TetherX","SerialX/Y","EthX/Y","vlanX","modemX"]
I was messing around with another regex, and my method is pretty confusing, he will be interested in any elegant solutions you guys think about.
Here is some code for it that could be improved, as well as set does not preserve order, so it must also be improved:
a = ["Radio0","Tether0","Serial0/0","Eth0/0","Eth0/1","Eth0/2","Eth1/0","vlanX","modem0","modem1","modem2","modem3","modem6"] c =[] for i in a: b = re.split("[0-9]", i) if "/" in i: c.append(b[0]+"X/Y") elif len(b) > 1: c.append(b[0]+"X") else: c.append(b) print set(c) set(['modemX', 'TetherX', 'RadioX', 'vlanX', 'SerialX/Y', 'EthX/Y'])
Possible enhancement when typing to maintain order:
unique=[] [unique.append(item) for item in c if item not in unique] print unique ['RadioX', 'TetherX', 'SerialX/Y', 'EthX/Y', 'vlanX', 'modemX']