python collections.defaultdict()编译错误

时间:2011-08-15 20:17:05

标签: python dictionary defaultdict

以下代码简单而清晰,在编译时会产生错误:

import string
import collections

#create dictionary with alphabets as keys, and empty values
list = ['aema', 'airplane', 'amend']

gen_dict = dict.fromkeys(string.ascii_lowercase, '')

gen_dict = collections.defaultdict(list)

for x in list:
    gen_dict['a'].append(x)

,产生的错误是:

Traceback (most recent call last):
  File "indexdict.py", line 14, in <module>
    gen_dict = collections.defaultdict(list)
TypeError: first argument must be callable

任何想法? 提前谢谢

1 个答案:

答案 0 :(得分:12)

您使用上面的list覆盖作为类型名称的内部list = ['aema', 'airplane', 'amend']。将您的list重命名为例如keyskeylist,一切都会好的。

所以替换

list = ['aema', 'airplane', 'amend']

keys = ['aema', 'airplane', 'amend']

for x in list:

for x in keys: