python collections.defaultdict()编译错误 [英] python collections.defaultdict() compile error

查看:113
本文介绍了python collections.defaultdict()编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code, simple and clear enough, produces an error when compiled:

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



<任何想法?
提前感谢

any idea? thanks in advance

推荐答案

您覆盖内部列表作为一种类型的名称,您的 list = ['aema','airplane','revision'] 。将列表重命名为 keylist ,一切都会很好。

you overwrite the internal list, being the name of a type, with your list = ['aema', 'airplane', 'amend'] above. Rename your list to e.g. keys or keylist and all will be fine.

所以更换

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

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

for x in list:

with

for x in keys:

这篇关于python collections.defaultdict()编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆