Python 3.5 TypeError 上的 Gensim 1.0.1:“map”类型的对象没有 len()?

THN*_*THN 2 python python-3.x gensim anaconda

我使用 Anaconda 安装了 Python 3.5,使用 pip 安装了 gensim 1.0.1(支持 Python 3)。运行gensim时出现以下错误:

Exception in thread Thread-61:
Traceback (most recent call last):
  File "/Users/mac/anaconda/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/Users/mac/anaconda/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 838, in job_producer
    sentence_length = self._raw_word_count([sentence])
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 755, in _raw_word_count
    return sum(len(sentence) for sentence in job)
  File "/Users/mac/anaconda/lib/python3.5/site-packages/gensim/models/word2vec.py", line 755, in <genexpr>
    return sum(len(sentence) for sentence in job)
TypeError: object of type 'map' has no len()
Run Code Online (Sandbox Code Playgroud)

导致此错误的代码来自node2vec。我正在将其移植到 Python 3,但出现此错误。

我知道在 Python 3 中,len(map) 会导致错误,这是否意味着 Gensim 1.0.1 不支持 Python 3,尽管pip 网站说它支持?还是有什么隐藏的设置?

任何人都知道出了什么问题?谢谢。

Kar*_*roo 5

当然,Gensim 支持 Python 3。您(或 node2vec)有责任提供Word2Vec()可迭代的句子。

在这种情况下,您必须向它传递一个包含步行的可迭代对象 - 其中每个步行都是一个顶点列表:

walks = [list(map(str, walk)) for walk in walks] # convert each vertex id to a string
model = Word2Vec(walks, ...)
Run Code Online (Sandbox Code Playgroud)