返回元素字典,而不是python参数中的一个元素

问题描述 投票:0回答:1

我有文字corpus

text = "natural language processing and machine learning is fun and exciting"

corpus = [[word.lower() for word in text.split()]]

我在课堂上有职能,这给我返回了单词的向量:

def word_vec(self, word):
    w_index = self.word_index[word]
    v_w = self.w1[w_index]
    return v_w

例如使用此代码:

word = "machine" vec = w2v.word_vec(word) print(word, vec)

我有这个输出:

machine [ 0.76702922 -0.95673743 0.49207258 0.16240808 -0.4538815 -0.74678226 0.42072706 -0.04147312 0.08947326 -0.24245257]

我如何获得语料库中所有单词的输出,而不仅仅是我在w2v.word_vec(word)的参数中使用的单词

我需要在函数内部进行更改,而不仅仅是print更改

我想我需要以格式dictionaryall words]的形式返回[C0

python
1个回答
0
投票

您可以使用all vectors

dict comprehension

或者如果您描述的vecs = {word: w2v.word_vec(word) for word in corpus} 是嵌套列表,则为... in corpus[0]

© www.soinside.com 2019 - 2024. All rights reserved.