首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python多个列表中找到给定值的索引?

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

Stack Overflow用户
提问于 2014-09-19 09:45:26
回答 2查看 165关注 0票数 0

下面给出了一个示例多个列表,我正在与之合作。现在我需要找到给定值的indices,如果它出现在列表中的任何地方。对于lis[0][2][2][1][0] which is 'span 1 2',我想把索引作为[0,2,2,1,0]

代码语言:javascript
复制
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源代码修改)。

代码语言:javascript
复制
from copy import copy
def scope2(word, list, indexes = None):
    flag = 0
    result = []
    if not indexes:
        indexes = []
    for index, item in enumerate(list):
        try:
            current_index = indexes + [index]
            result.append(current_index + [item.index(word)])
        except ValueError:
            pass
        print item
        print str(current_index) + ":::::" + str(list.index(item))
        for stuff in item:        
            if type(stuff) == type([]):
                flag =1 

    if flag==1:
        indexes.append(index)
        result.extend(scope2(word, item, copy(indexes)))

return result

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

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等等。可能的问题是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-19 10:30:35

代码语言:javascript
复制
>>> 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)
>>> 
票数 2
EN

Stack Overflow用户

发布于 2014-09-19 10:41:18

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

代码语言:javascript
复制
>>> 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]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25931002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档