Python:嵌套{dict:{dict:[list]}}结构和正则表达式匹配的问题

时间:2016-02-11 22:58:54

标签: python regex list dictionary

我一直试图弄清楚我在嵌套词典/列表中遇到的问题。

import re
sentence_list = ["the quick brown fox", "ellie the elephant", "the lion, the witch and the wardrobe", "lion and tiger and elephant, oh my!"]

animal_dict = {"lion":[], "fox":[], "tiger":[], 'elephant':[]}

sentence_dict = {}

for s in sentence_list:
    sentence_dict[s] = animal_dict

for sentence in sentence_dict:
    for w in sentence_dict[sentence]:    
        for m in re.finditer(w,sentence):   
            sentence_dict[sentence][w].append(m.start(0))


print sentence_dict

它给了我以下输出,即它将值附加到dict中每个句子的每个列表中,而不仅仅是当前的那个:

{'the quick brown fox': {'tiger': [9], 'lion': [4, 0], 'fox': [16], 'elephant': [19, 10]}, \
'the lion, the witch and the wardrobe': {'tiger': [9], 'lion': [4, 0], 'fox': [16], 'elephant': [19, 10]}, \
'lion and tiger and elephant, oh my!': {'tiger': [9], 'lion': [4, 0], 'fox': [16], 'elephant': [19, 10]}, \
'ellie the elephant': {'tiger': [9], 'lion': [4, 0], 'fox': [16], 'elephant': [19, 10]}}

有关如何解决此问题的任何建议?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我假设您希望输出显示每个句子中出现动物名称的索引。

import re
sentence_list = ["the quick brown fox", "ellie the elephant", "the lion, the witch and the wardrobe", "lion and tiger and elephant, oh my!"]

animal_list = ["lion", "fox", "tiger", 'elephant']

sentence_dict = {}

for s in sentence_list:
  sentence_dict[s] = {}
  for a in animal_list:
    sentence_dict[s][a] = []

for sentence in sentence_dict:
    for w in animal_list:
        for m in re.finditer(w, sentence):
            sentence_dict[sentence][w].append(m.start(0))


print sentence_dict

上面的代码有以下输出:

{'the quick brown fox': {'tiger': [], 'lion': [], 'fox': [16], 'elephant': []}, 'the lion, the witch and the wardrobe': {'tiger': [], 'lion': [4], 'fox': [], 'elephant': []}, 'lion and tiger and elephant, oh my!': {'tiger': [9], 'lion': [0], 'fox': [], 'elephant': [19]}, 'ellie the elephant': {'tiger': [], 'lion': [], 'fox': [], 'elephant': [10]}}

你的代码不起作用的原因是因为你的animal_dict中的列表是同一个对象,因此不是为每个句子/动物对保留一个单独的列表,而是每个动物在句子中共享相同的列表。

相关问题