![](/img/trans.png)
[英]descriptor 'values' requires a 'dict' object but received a 'int'/'str' python
[英]descriptor 'get' requires a 'dict' object but received a 'generator'
这是我的代码。我在此代码行中遇到错误。
inverse_vocabulary = dict.get((word, i) for i, word in enumerate(vocabulary))
我的错误是:
TypeError Traceback (most recent call last)
> <ipython-input-27-7941d5453736> in <module>()
> 21
> 22 vocabulary = open(r"G:\final year
> project\data/vocabulary.txt").read().split("\n")
> ---> 23 inverse_vocabulary = dict.get((word, i) for i, word in
> enumerate(vocabulary))
> 24
> 25 TypeError: descriptor 'get' requires a 'dict' object but
received a
> 'generator'
首先,愚蠢的细节,但 Python 不是编译语言 - 它是一种解释语言 - 仅供您以后参考
其次,正如评论者所说 - 您将生成器传递给 dict.get()。 将该生成器包装在 dict() 调用中以将其计算为字典。
inverse_vocabulary = dict.get(dict((word, i) for i, word in enumerate(vocabulary)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.