类型错误 - 应该是字典中的索引?

时间:2013-02-13 18:25:12

标签: python dictionary

def __init__(self, dictionary_paths):
    files = [open(path, 'r') for path in dictionary_paths]
    dictionaries = [yaml.load(dict_file) for dict_file in files]
    map(lambda x: x.close(), files)
    self.dictionary = {}
    self.max_key_size = 0
    for curr_dict in dictionaries:
        for key in curr_dict:
            if key in self.dictionary:
                self.dictionary[key].extend(curr_dict[key])
            else:
                self.dictionary[key] = curr_dict[key]
                self.max_key_size = max(self.max_key_size, len(key))
    self.dictionary[key] = curr_dict[key]

结果

TypeError: string indices must be integers, not str

如何解决?

1 个答案:

答案 0 :(得分:1)

问题是curr_dict是一个字符串,而不是一个字典,尽管它的名字暗示:

In [6]: 'abc'['x']

TypeError: string indices must be integers, not str