gpt4 book ai didi

python - TypeError: 'map' object is not subscriptable 错误在 Python 3

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:11 25 4
gpt4 key购买 nike

我正在尝试在 Python 中使用作为 NLTK 一部分的 FreqDist。我试过这个示例代码:

fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
vocabulary1[:50]

但是最后一行给我这个错误:

TypeError: 'map' object is not subscriptable

我认为代码在 Python 2 上运行良好,但在 Python 3(我拥有的)上却出现上述错误。

为什么会出现这个错误,如何解决?感谢您对此提供的任何帮助。

最佳答案

在 Python 3 中,.keys() 返回一个迭代器,您不能对它进行切片。在切片之前将其转换为列表。

fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
x = list(vocabulary1)[:50]
# or...
vocabulary1 = list(fdist1.keys())
x = vocabulary1[:50]

关于python - TypeError: 'map' object is not subscriptable 错误在 Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com