How to organise specially formatted text to a list (on python) <p>I've got data with flight routing codes. It has lot's of strings like this:</p> <pre><code>routing = 'PBI-FLL/FMY/JAX/MIA/ORL-PNS/TPA-SRQ-CLE/CHI/HOU/WAS-DEN-ELP' </code></pre> <p>I need to get lists with this strings like this: </p> <pre><code>routinglist = [['PBI'], ['FLL','FMY','JAX','MIA','ORL'], ['PNS','TPA'], ['SRQ'], ['CLE','CHI','HOU','WAS'], ['DEN']] </code></pre> <p>I wrote this code but it is to complicated and doesn't work as needed</p> <pre><code>routingrules = 'PBI-FLL/FMY/JAX/MIA/ORL-PNS/TPA-SRQ-CLE/CHI/HOU/WAS-DEN-ELP' airports = [] nn = 0 few = '' airportcount = 0 for simvol in routingrules: if (nn) % 4 == 0: previous = routingrules[nn:nn+3] if routingrules[nn+3:nn+4] == '/': few = few + previous + "1,2" elif routingrules[nn+3:nn+4] == '-': if few != '': airports.append([few + previous]) airportcount = airportcount+1 few = '' else: airports.append([previous]) airportcount = airportcount+1 else: if few != '': airports.append([few + previous]) airportcount = airportcount+1 few = '' nn = nn+1 nn = nn+1 print airports </code></pre> <p>it prints </p> <pre><code>[['PBI'], ['FLL1,2FMY1,2JAX1,2MIA1,2ORL'], ['PNS1,2TPA'], ['SRQ'], ['CLE1,2CHI1,2HOU1,2WAS'], ['DEN']] </code></pre>