【发布时间】:2015-06-07 18:47:54
【问题描述】:
我正在尝试创建一个函数,该函数查看多个括号并从内到外返回每组括号的内容。 所以给定输入 (9*(2*(6*6))),它会返回
(6*6)
(2*(6*6))
(9*(2*(6*6)))
到目前为止我有这个,但我不确定如何使它适用于多对括号。它只返回最里面的括号。
def in_brackets(str) :
close_b = 0
open_b = 0
if len(str) < 5 :
return True
while str[close_b] != ')':
close_b += 1
if str[close_b] == '(':
open_b = close_b
in_b = str[open_b:close_b + 1]
return(in_b)
【问题讨论】:
-
输入"((1+2)*(3+4))`应该返回什么?
-
只会返回 (1+2)*(3+4)
-
看看this answer(pyparsing)。
标签: python python-3.x recursion iteration