以下代码简单而清晰,在编译时会产生错误:
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
任何想法? 提前谢谢
答案 0 :(得分:12)
您使用上面的list
覆盖作为类型名称的内部list = ['aema', 'airplane', 'amend']
。将您的list
重命名为例如keys
或keylist
,一切都会好的。
所以替换
list = ['aema', 'airplane', 'amend']
与
keys = ['aema', 'airplane', 'amend']
和
for x in list:
与
for x in keys: