题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
参考,感谢: :
https://blog.nowcoder.net/n/dbec571fda1244d09560c54f96a7448e
while True:
try:
str_1=input()
stack=[]
list_1=[]
count=0
for i in str_1:
if i=='"' and '"' in stack:
list_1.append("".join(stack))
count+=1
stack=[]
elif i==' ' and '"' not in stack:
if len(stack)!=0: #!!!如果避免字符串开头就是一个空格却满足出栈的情况
list_1.append("".join(stack))
count+=1
stack=[]
else:
stack.append(i)
if len(stack)!=0: #新规范的for循环最后一次迭代撞墙的正确办法,先判断待迭代的序列是否还有子弹
list_1.append("".join(stack))
count+=1
stack=[]
print(count)
for j in list_1:
if '"' in j :
print(j[1:]) # 为什么不是[1:-1]:因为第二个“ 并没有入栈,而是作为出栈条件,出栈的字符串其实只包含[0]的"
else:
print(j)
except:
break
# # elif i==' ' and '"' not in stack:
# if len(stack)!=0:
# 与 elif i==' ' and '"' not in stack and if len(stack)!=0 :
# 有何区别呢:
# 如果两个单词直接有两个空格,第一个空格满足该条件弹出,但是第二个空格因为不满足该elif 条件
# 所以被划分到else的操作中,也就给新栈增加了一个空格,导致输出格式不对。 
