【发布时间】:2019-04-24 10:08:32
【问题描述】:
我希望能够转动 字符串 例如:'(* (+ int (+ int real)) int)'
到嵌套的 list 中,其中括号是列表的开始/结束,看起来像这样(在这种情况下)
['*', ['+', 'int', ['+', 'int', 'real']], 'int']
我尝试了以下代码,但它不起作用
def bracketCheck(el):
if el == ')' or el == '(':
return False
else:
return True
def stringIntoList(lst):
lst1 = ''
lst2 = []
for i in range(0, len(lst)-1):
if bracketCheck(lst[i]):
lst1 += lst[i]
elif lst[i] == '(':
b = stringIntoList(lst[i:])
elif lst[i] == ')':
lst2.append(lst1)
lst2.append(b)
lst1 = ''
return lst2
【问题讨论】:
-
不反对,但可以通过解决这个 (idownvotedbecau.se/itsnotworking) 常见问题的某些元素来改进问题。
标签: python string list for-loop recursion