"TypeError:"NoneType"对象不可调用"将列表添加到字典时 [英] "TypeError: 'NoneType' object is not callable" when adding lists to dictionary

查看:108
本文介绍了"TypeError:"NoneType"对象不可调用"将列表添加到字典时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a function that consolidates a couple of lists into a string and am encountering the below error.

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

任何人都可以提出可能的解决方案,因为我已经花了很长时间了,但还没有弄清楚.谢谢.

Would anyone be able to suggest a possible fix as I've spent quite a while on this and haven't figured it out yet. Thanks.

忘了提及我正在使用Python 3

Forgot to mention I'm using Python 3

推荐答案

如果您使用的是Python 2,则mapdict都将绑定到None.检查代码的 rest 中是否有分配给任何名称的

If you are using Python 2, then either map or dict is bound to None. Check the rest of your code for assignments to either name.

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

Note that instead of map(None, iterable1, iterable2) you can use zip(iterable1, iterable2) to get the same output.

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

If you are using Python 3, then the map() method does not support None as a first argument:

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

,您当然想在其中使用zip():

and you certainly want to use zip() there:

defsTwo = dict(zip(letters, defsOne))

这篇关于"TypeError:"NoneType"对象不可调用"将列表添加到字典时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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