![](/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.