3

以下代码足够简单明了,编译时会产生错误:

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

任何的想法?提前致谢

4

1 回答 1

12

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

所以更换

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

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

for x in list:

for x in keys:
于 2011-08-15T20:20:21.587 回答