擅长:python、mysql、java
<p>试试这个,如果我正确理解你的问题,这就是你想要的。
在这里,我在搜索字符串中的“(”和“)”之后,在字典中添加值</p>
<pre><code>string = "(foo(bar))"
print(string.find(")"))
num = string.count(")")
a = 0
b = 0
dict_ = {"open": [], "close": []}
a = string.find("(", a)
dict_["open"].append(a)
for _ in range(0, num):
a = string.find("(", a+1)
dict_["open"].append(a)
b = string.find(")", b+1)
dict_["close"].append(b)
dict_["open"].pop(-1)
print(dict_)
</code></pre>