我有航班路线代码的数据。它有很多这样的字符串:
routing = 'PBI-FLL/FMY/JAX/MIA/ORL-PNS/TPA-SRQ-CLE/CHI/HOU/WAS-DEN-ELP'
我需要使用这样的字符串获取列表:
routinglist = [['PBI'], ['FLL','FMY','JAX','MIA','ORL'], ['PNS','TPA'], ['SRQ'], ['CLE','CHI','HOU','WAS'], ['DEN']]
我写了这段代码,但它很复杂,不能按需要工作
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
它打印
[['PBI'], ['FLL1,2FMY1,2JAX1,2MIA1,2ORL'], ['PNS1,2TPA'], ['SRQ'], ['CLE1,2CHI1,2HOU1,2WAS'], ['DEN']]