首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较列表,如果列表匹配,则存储索引值

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

Stack Overflow用户
提问于 2016-04-03 19:43:50
回答 1查看 53关注 0票数 0

我有两份名单:

  • 字指示语
  • list2 词组=“某处”、“过去”、“彩虹”、“蓝鸟”、“飞”、“鸟”、“飞”、“过去”、“彩虹”、“为什么”、“为什么”、“双”、“时间”、“人口”、“长”、“取”、“人口”、“双倍”、“大小”、“哇”、“满”、“彩虹”、“路”、“双”、“彩虹”、“双”,“彩虹”、“list2 =(某处)”、“过去”、“彩虹”、“蓝鸟”、“飞”、“鸟”、“飞”、“过去”、“彩虹”、“为什么”、“为什么”、“双倍”、“时间”、“人口”、“长”、“采取”、“人口”、“双”、“大小”、“哇”、“满”、“彩虹”、“方式”、“双”,“彩虹”、“双重”、“彩虹”、“道路”]

我使用中的单词(删除重复项)来表示,并查看它们是否包含在list2中。如果是,我希望取wordsindict.中单词的索引值。下面是我目前拥有的代码:

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

此代码生成以下输出:

代码语言:javascript
复制
{'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], []]}

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

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

代码语言:javascript
复制
                     ^     ^     ^
                     0     1     2

在这里您可以看到,填充确实出现在第一个索引值中,而是记录在第二个子列表中的单词索引值,而不是简单的'population': [1, 1]。

简单地说,我希望添加来自list2 (0-2)的索引值,如果来自list2的单词确实多次出现在list2中,那么从找到它的位置再追加索引值。

字指示符包含它们的键,应该搜索list2以查找出现的情况。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-03 20:00:01

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

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

产出:

代码语言:javascript
复制
{'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]}

如果您希望列表索引中的位置在该列表中

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

https://stackoverflow.com/questions/36390268

复制
相关文章

相似问题

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