下面给出的是一个示例多列表,我正在处理。现在我需要找到给定值的indices
,如果它出现在列表中的任何地方。例如:对于lis[0][2][2][1][0] which is 'span 1 2'
,我想得到的索引是[0,2,2,1,0]
。在
lis = [['Root', ['span 1 6'], ['Nucleus', ['span 1 3'], ['Nucleus', ['span 1 2'], ['Nucleus', ['leaf 1'], ['text', "Specific knowledge "]], ['Satellite', ['leaf 2'], ['rel2par Elaboration'], ['text', 'required !_']]], ['Satellite', ['leaf 3'], ['rel2par Elaboration'], ['text', 'The tester is ']]], ['Satellite', ['span 4 6'], ['rel2par Elaboration'], ['Satellite', ['leaf 4'], ['rel2par Attribution'], ['text', 'For instance , the tester is aware!_']], ['Nucleus', ['span 5 6'], ['Nucleus', ['leaf 5'], ['rel2par Contrast'], ['text', 'that a!_']], ['Nucleus', ['leaf 6'], ['rel2par Contrast'], ['text', 'but ']]]]]]
我尝试了以下方法(从网络来源修改)。在
^{pr2}$这里的问题是,sibling(同一级别的列表)的索引也会返回,但并不总是这样。一些示例输出如下所示
for 0,2,3,1 it returns 0,2,3,1,0, similarly for lis[0][2][3][4][3][3] it returns 0,2,3,3,4,3,3
等等。可能的问题是什么?在
下面是深度优先和广度优先搜索的实现,不限于字符串。搜索一个列表或元组需要一些修改。在
相关问题 更多 >
编程相关推荐