Python,在列表的列表中查找括号,并且return索引,以及括号

Python, find parantheses in a list of list, and return the index, and the parantheses

我试图在一个矩阵(列表的列表)中找到所有的括号(及其内容)。我想在列表中包含括号索引和内容。例如: ['bla bla bla ('先生a')bla','bla bla','bla bla bla ('先生b')] 我想 : [[0, 'Mr a'], [2, 'Mr b']] 谢谢

你可以这样做:

list1 = ['bla bla bla ( Mr a ) bla', 'bla bla', 'bla bla bla ( Mr b )']
res = []
for i in range(len(list1)):
    ind1 = list1[i].find('(')
    ind2 = list1[i].find(')')
    if ind1 < ind2:
        res.append(list1[i][ind1+1:ind2])
print(res)

print(res)给出

[' Mr a ', ' Mr b ']