如何在python多列表中查找给定值的索引?

如何在python多列表中查找给定值的索引?,python,Python,下面是我正在处理的多列表示例。现在,如果给定值出现在列表中的任何位置,我需要找到该值的索引。例如:对于lis[0][2][2][1][0],即“span 12”,我希望将索引设置为[0,2,2,1,0] lis = [['Root', ['span 1 6'], ['Nucleus', ['span 1 3'], ['Nucleus', ['span 1 2'], ['Nucleus', ['leaf 1'], ['text', "Specific knowledge "]], ['Sat

下面是我正在处理的多列表示例。现在,如果给定值出现在列表中的任何位置,我需要找到该值的
索引。例如:对于
lis[0][2][2][1][0],即“span 12”
,我希望将索引设置为
[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 ']]]]]]
我尝试了以下方法(从web源代码修改)

这里的问题是同级索引(同级列表)也会返回,但并不总是返回。一些示例输出如下


对于0,2,3,1,它返回0,2,3,1,0;对于lis[0][2][3][4][3][3],它同样返回0,2,3,3,4,3,3,依此类推。可能的问题是什么?

这里是深度优先和广度优先搜索的实现,不限于字符串。搜索列表或元组需要一些修改

>>> def trail(word, lst):
...     if word in lst:
...         return [lst.index(word)]
...     for i, x in enumerate(lst):
...         if not isinstance(x, list):
...             continue
...         ret = trail(word, x)
...         if ret is not None:
...             return [i] + ret
... 
>>> trail('span 1 2', lis)
[0, 2, 2, 1, 0]
>>> lis[0][2][2][1][0]
'span 1 2'
>>> trail('no such string', lis)
>>> 
>>> l = [['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 ']]]]]]
>>> def depth_first(term,data):
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       r = depth_first(term,item)
...       if not r is None:
...         return [i] + r
...     else:
...       if item == term:
...         return [i]
...
>>> def breadth_first(term,data):
...   later = []
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       later.append((i,item))
...     else:
...       if item == term:
...         return [i]
...   for i, item in later:
...     r = breadth_first(term,item)
...     if not r is None:
...       return [i] + r
>>> depth_first('span 1 2',l)
[0, 2, 2, 1, 0]
>>> breadth_first('span 1 2',l)
[0, 2, 2, 1, 0]

这里是深度优先和广度优先搜索的实现,不限于字符串。搜索列表或元组需要一些修改

>>> l = [['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 ']]]]]]
>>> def depth_first(term,data):
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       r = depth_first(term,item)
...       if not r is None:
...         return [i] + r
...     else:
...       if item == term:
...         return [i]
...
>>> def breadth_first(term,data):
...   later = []
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       later.append((i,item))
...     else:
...       if item == term:
...         return [i]
...   for i, item in later:
...     r = breadth_first(term,item)
...     if not r is None:
...       return [i] + r
>>> depth_first('span 1 2',l)
[0, 2, 2, 1, 0]
>>> breadth_first('span 1 2',l)
[0, 2, 2, 1, 0]

这里是深度优先和广度优先搜索的实现,不限于字符串。搜索列表或元组需要一些修改

>>> l = [['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 ']]]]]]
>>> def depth_first(term,data):
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       r = depth_first(term,item)
...       if not r is None:
...         return [i] + r
...     else:
...       if item == term:
...         return [i]
...
>>> def breadth_first(term,data):
...   later = []
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       later.append((i,item))
...     else:
...       if item == term:
...         return [i]
...   for i, item in later:
...     r = breadth_first(term,item)
...     if not r is None:
...       return [i] + r
>>> depth_first('span 1 2',l)
[0, 2, 2, 1, 0]
>>> breadth_first('span 1 2',l)
[0, 2, 2, 1, 0]

这里是深度优先和广度优先搜索的实现,不限于字符串。搜索列表或元组需要一些修改

>>> l = [['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 ']]]]]]
>>> def depth_first(term,data):
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       r = depth_first(term,item)
...       if not r is None:
...         return [i] + r
...     else:
...       if item == term:
...         return [i]
...
>>> def breadth_first(term,data):
...   later = []
...   for i, item in enumerate(data):
...     if isinstance(item,Sequence) and not isinstance(item,basestring):
...       later.append((i,item))
...     else:
...       if item == term:
...         return [i]
...   for i, item in later:
...     r = breadth_first(term,item)
...     if not r is None:
...       return [i] + r
>>> depth_first('span 1 2',l)
[0, 2, 2, 1, 0]
>>> breadth_first('span 1 2',l)
[0, 2, 2, 1, 0]

lis[0][2][3][1]
['leaf 3']
lis[0][2][2][1][0]
'span 12'
@falsetru抱歉,它只是lis[0][2][2][1][0]。在问题
lis[0][2][3][1]
中编辑的是
['leaf 3']
lis[0][2][2][1][0]
'span 12'
@falsetru抱歉,它只是lis[0][2][2][1][0]。在问题
lis[0][2][3][1]
中编辑的是
['leaf 3']
lis[0][2][2][1][0]
'span 12'
@falsetru抱歉,它只是lis[0][2][2][1][0]。在问题
lis[0][2][3][1]
中编辑的是
['leaf 3']
lis[0][2][2][1][0]
'span 12'
@falsetru抱歉,它只是lis[0][2][2][1][0]。在问题中编辑