如下图所示,顺便说一下可能的报错问题:
image.png
问题:
TypeError: 'dict' object is not callable
原因:
dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用dict()函数是会报出“TypeError: 'dict' object is not callable”的错误,只需将之前自定义的变量delete掉即可。
解决办法:
>>>del (dict)
修改之后
image.png小项目:如何给《三国演义》中的部分名称编号
#给关键字添加对应值
#del (dict)这里无需删除字典内置函数,因为下面已经定义好了
excludes={"将军","却说","二人","后主","上马","不知","天子","大叫","众将","不可",
"主公","蜀兵","只见","如何","商议","都督","一人","汉中","不敢","人马",
"陛下","魏兵","天下","今日","左右","东吴","于是","荆州","不能","如此",
"大喜","引兵","次日","军士","军马"}
keys=[]
values=[]
for i in range(len(excludes)):
values.append(i+1)
for i in excludes:
keys.append(i)
dictionary = dict(zip(keys, values))
print(dictionary)
image.png
网友评论