2013-02-13 35 views
-1
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

什麼是curr_dict?什麼是字典?你究竟想要做什麼? – 2013-02-13 18:26:51

回答

1

的問題是curr_dict是一個字符串,而不是一個字典,儘管它的名字所暗示:

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

TypeError: string indices must be integers, not str 
相關問題