题解 | #参数解析#

参数解析

http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

Python递归解析
处理一点删一点,再处理再删,删完为止

mycommand = input()

def parse(mycommand, commands = []):
    indexLeft = mycommand.find('"')
    if indexLeft == -1: #找不到直接返回
        commands += mycommand.split(' ')
        return commands
    else: #找到处理完再递归
        commands += mycommand[:indexLeft - 1].split(' ') #先加引号前面的
        indexRight = mycommand.find('"', indexLeft + 1) #找右边的引号
        commands.append(mycommand[indexLeft + 1:indexRight]) #加引号中间的
        if indexRight == len(mycommand) - 1: #后面没了直接返回,防止越界
            return commands
        mycommand = mycommand[indexRight + 1:] #处理了的删了再递归
        return parse(mycommand, commands)

commands = parse(mycommand)
commands = [i for i in commands if i != ''] #去掉空值
print(len(commands))
for i in commands:
    print(i)
全部评论

相关推荐

代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务