我试图做一个函数,通过多个括号查看并从内部返回每个括号的内容。 所以给出的输入(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))'返回 – Kevin 2015-04-02 17:51:02
,将刚刚返回(1 + 2) *(3 + 4) – iotaa 2015-04-02 17:54:26
看看[这个答案](http://stackoverflow.com/a/1657068/2072035)(pyparsing)。 – saaj 2015-04-02 18:18:56