简体   繁体   English

如果列表匹配,则比较列表并存储索引值

[英]comparing lists and storing index values if lists match

I have two lists: 我有两个清单:

  • wordsindict 文字指示
  • list2 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']] 

I am taking the words(removing duplicates) that are within wordsindict and seeing if they are contained within list2. 我正在wordsindict中的单词(删除重复项),并查看它们是否包含在list2中。 If they are, I wish to take the index value of the word in wordsindict . 如果是的话,我希望在wordsindict中获取单词的索引值 Beneath is the code that I currently have: 下面是我目前拥有的代码:

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)

this code produces this output: 此代码产生以下输出:

{'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], []]}

it takes the words from wordsindict and stores their index value. 它从wordsindict中提取单词并存储其索引值。 This is incorrect as there are only 3 sublists within list2. 这是不正确的,因为list2中只有3个子列表。 It gives each index value its own list: 它为每个索引值提供自己的列表:

eg 'population': [[], [2, 5], [] 例如 'population': [[], [2, 5], []

                     ^     ^     ^
                     0     1     2

Here you can see that population does appear within the first index value, but instead the words index value within the second sublist is recorded instead of simply 'population': [1, 1] . 在这里,您可以看到总体确实出现在第一个索引值中,但是记录了第二个子列表中的单词索引值,而不是简单地记录了'population': [1, 1]

Put simply, I want the index value from list2 (0-2) to be appended, and if the word from wordsindict does appear more than once in list2 then append the index value again from where it was found. 简而言之,我想要附加list2(0-2)的索引值,并且如果wordsindict中的单词确实在list2中出现多次,则再次从找到的位置附加索引值。

wordsindict contains they keys and list2 should be searched for the occurrences. wordindict包含它们的关键字,应该搜索list2以查找出现的情况。

If you need any more information, please do not hesitate to ask! 如果您需要更多信息,请随时询问!

If I understand the question correctly I think this is what you were looking for: 如果我正确理解了这个问题,我认为这就是您想要的:

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)

output: 输出:

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

If you want the list index with the location in that list 如果您想要列表索引以及该列表中的位置

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM