如何将特殊格式的文本组织到列表中(在python上)

时间:2013-02-20 14:33:40

标签: python string algorithm list

我有飞行路线代码的数据。 它有很多字符串:

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']]

2 个答案:

答案 0 :(得分:4)

-上拆分,然后在/上分组:

[r.split('/') for r in routing.split('-')]

输出:

>>> routing = 'PBI-FLL/FMY/JAX/MIA/ORL-PNS/TPA-SRQ-CLE/CHI/HOU/WAS-DEN-ELP' 
>>> [r.split('/') for r in routing.split('-')]
[['PBI'], ['FLL', 'FMY', 'JAX', 'MIA', 'ORL'], ['PNS', 'TPA'], ['SRQ'], ['CLE', 'CHI', 'HOU', 'WAS'], ['DEN'], ['ELP']]

与你的例子相比,这是一个额外的['ELP']条目,但我认为这是一个疏忽。很容易将其删除。

答案 1 :(得分:1)

虽然这个问题可能过度,但pyparsing在这里运作良好。

routing = 'PBI-FLL/FMY/JAX/MIA/ORL-PNS/TPA-SRQ-CLE/CHI/HOU/WAS-DEN-ELP' 

from pyparsing import *
block   = Group(delimitedList(Word(alphas), '/'))
grammar = Group(delimitedList(block, '-'))

print grammar.parseString(routing)

打印:

[[['PBI'], ['FLL', 'FMY', 'JAX', 'MIA', 'ORL'], ['PNS', 'TPA'], ['SRQ'], ['CLE', 'CHI', 'HOU', 'WAS'], ['DEN'], ['ELP']]]

当您想要扩展语法时,优势就来了。通常这些问题随着时间的推移开始变得简单并且复杂性增加,这里将语法与解析分开是有用的。