将列表添加到字典时出现“ TypeError:'NoneType'对象不可调用”

End*_*ide 1 python dictionary

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

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
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

# 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
Run Code Online (Sandbox Code Playgroud)

我花了很长时间仍未弄清楚,谁能提出建议的修复方法。谢谢。

编辑:忘记提及我正在使用Python 3

Mar*_*ers 5

如果您使用的是Python 2,则mapdict绑定到None。检查代码的其余部分,以获取对这两个名称的分配。

请注意,map(None, iterable1, iterable2)您可以使用而不是zip(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
Run Code Online (Sandbox Code Playgroud)

并且您当然想在这里使用zip()

defsTwo = dict(zip(letters, defsOne))
Run Code Online (Sandbox Code Playgroud)