Skip to main content
 首页 » 编程设计

python之"TypeError: ' NoneType ' object is not callable"添加列表到字典时

2025年05月04日14myhome

我正在创建一个将几个列表合并为一个字符串的函数,但遇到了以下错误。

Traceback (most recent call last): 
  File "TraditionalRoute\BioKeywords.py", line 65, in <module> 
    print(PrintKeyDefs()) 
  File "TraditionalRoute\BioKeywords.py", line 30, in PrintKeyDefs 
    defsTwo = dict(map(None, letters, defsOne)) 
TypeError: 'NoneType' object is not callable 

我的代码如下:

# print keyword and three definitions, one of which is the correct definition 
def PrintKeyDefs(): 
   print('\nRandomly selected keyword:',SelectKeyword(),'\n') 
   # definitions below 
   defsOne = [] 
   defsOne.append(keywords[ChosenKeyword]) # choosing the keyword 
   RandDefCount = 0 
   while RandDefCount < 2: # adding two random keywords to the list 
      defsOne.append(keywords[random.choice(words)]) 
      RandDefCount += 1 
   random.shuffle(defsOne) # randomizing the keywords 
   letters = ['A) ','B) ','C) '] 
   defsTwo = dict(map(None, letters, defsOne)) # trying to put them together in a dict. the problem is here 
   defsThree = '' 
   defsThree += '\n'.join(defsTwo) # changing to a string 
   return defsThree 

有没有人能提出一个可能的修复建议,因为我已经在这上面花了很长时间,但还没有弄清楚。谢谢。

编辑:忘了说我正在使用 Python 3

请您参考如下方法:

如果您使用的是 Python 2,则 map 或 dict 将绑定(bind)到 None。检查您的代码的其余 是否分配给任一名称。

请注意,您可以使用 zip(iterable1, iterable2) 代替 map(None, iterable1, iterable2) 来获得相同的输出。

如果您使用的是 Python 3,则 map() 方法不支持将 None 作为第一个参数:

>>> list(map(None, [1], [2])) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
TypeError: 'NoneType' object is not callable 

你当然想在那里使用 zip():

defsTwo = dict(zip(letters, defsOne))