2014-09-19 3 views
0

Ниже приведен пример нескольких списков, с которыми я работаю. Теперь мне нужно найти indices данного значения, если оно отображается в любом месте списка. Например, для lis[0][2][2][1][0] which is 'span 1 2', я хотел бы получить индекс как [0,2,2,1,0].Как найти индексы заданного значения в нескольких списках python?

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 ']]]]]] 

Я попробовал следующее (изменено с веб-источника).

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 и так далее. Какова может быть проблема?

+0

'lis [0] [2] [3] [1]' is '['leaf 3']'. 'lis [0] [2] [2] [1] [0]' is ''span 1 2''. – falsetru

+0

@falsetru извините, это только lis [0] [2] [2] [1] [0]. отредактирован в вопросе –

ответ

2
>>> 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) 
>>> 
1

Вот реализация первой и первой глубины поиска, не ограничиваясь строками. Требуется небольшая переработка для поиска списка или кортежа.

>>> 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] 
Смежные вопросы