2016-04-03 76 views
0

我有列表:比较列表和存储索引值,如果列表匹配

  • wordsindict
  • list2中

    wordsindict = ['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why', 'double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size', 'whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way'] 
    
    list2 = [['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why'], ['double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size'], ['whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way']] 
    

我正在采取的话(删除重复)那是在wordsindict,看看他们是否是容器d在list2内。如果是,我希望wordsindict索引值。下面是我目前拥有的代码:

listindex = {} 
for word in wordsindict: 
    listindex[word] = [] 
    for splittedLines_list in list2: 
     index_list = [] 
     for i,j in enumerate(splittedLines_list): 
      if j == word: 
       index_list.append(i) 
     listindex[word].append(index_list) 

这段代码产生这样的输出:

{'fly': [[4, 6], [], []], 'rainbow': [[2, 8], [], [2, 5, 7]], 'full': [[], [], [1]], 'bluebirds': [[3], [], []], 'takes': [[], [4], []], 'somewhere': [[0], [], []], 'double': [[], [0, 6], [4, 6]], 'over': [[1, 7], [], []], 'long': [[], [3], []], 'why': [[9, 10], [], []], 'whoa': [[], [], [0]], 'way': [[], [], [3, 8]], 'time': [[], [1], []], 'size': [[], [7], []], 'birds': [[5], [], []], 'population': [[], [2, 5], []]} 

它从wordsindict的话,并存储它们的索引值。这是不正确的,因为list2中只有3个子列表。它为每个索引值提供了自己的列表:

'population': [[], [2, 5], []

    ^ ^ ^
        0  1  2 

在这里你可以看到,它的人口第一指标值之内出现,而是在第二子列表中的单词索引值被记录的只是'population': [1, 1]代替。

简而言之,我想要追加来自list2(0-2)的索引值,并且如果来自wordsindict的单词在list2中出现多次,那么再次从发现它的位置追加索引值。

wordsindict包含它们的键和list2应该搜索的发生。

如果您需要更多信息,请不要犹豫,问问!

+0

dud,可以给其他SHORT例子吗? ,我不明白...你想要索引值在一个字典与搜索的名字? – Milor123

回答

1

如果我理解正确的问题,我想这就是你要找的人:

wordsindict = ['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why', 'double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size', 'whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way'] 

list2 = [['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why'], ['double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size'], ['whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way']] 
d = {} 
for word in set(wordsindict): 
    d[word] = [] 
    for i, l in enumerate(list2): 
     for wordy_word in l: 
      if wordy_word == word: 
       d[word].append(i) 
print(d) 

输出:

{'why': [0, 0], 'way': [2, 2], 'whoa': [2], 'full': [2], 'birds': [0], 'size': [ 
1], 'time': [1], 'long': [1], 'population': [1, 1], 'fly': [0, 0], 'somewhere': 
[0], 'takes': [1], 'rainbow': [0, 0, 2, 2, 2], 'bluebirds': [0], 'double': [1, 1 
, 2, 2], 'over': [0, 0]} 

如果你想在该列表中与位置列表索引

wordsindict = ['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why', 'double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size', 'whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way'] 

list2 = [['somewhere', 'over', 'rainbow', 'bluebirds', 'fly', 'birds', 'fly', 'over', 'rainbow', 'why', 'why'], ['double', 'time', 'population', 'long', 'takes', 'population', 'double', 'size'], ['whoa', 'full', 'rainbow', 'way', 'double', 'rainbow', 'double', 'rainbow', 'way']] 
d = {} 
for word in set(wordsindict): 
    d[word] = [] 
    for i, l in enumerate(list2): 
     for j, wordy_word in enumerate(l): 
      if wordy_word == word: 
       #new_d = {i: j} 
       #tuples probably better here 

       d[word].append((i, j)