题解 | #参数解析#
参数解析
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)