我认为您正在寻找的是您的话的数字表示。您可以使用 gensim 并将每个单词映射到一个令牌 id 并从中创建您的 numpy 数组,如下所示:
import numpy as np
from gensim import corpora
toconvert = [["aa bb","a","bbb","à"], ["bb", "cc","c","ddd","à"], ["kkk","a","","a"]]
# convert your list of lists into token id's. For example, 'aa bb' could be represented as a 2, a as a 1, etc.
tdict = corpora.Dictionary(toconvert)
# given nested structure, you can append nested numpy arrays
newlist = []
for l in toconvert:
tmplist = []
for word in l:
# append to intermediate list the id for the given word under observation
tmplist.append(tdict.token2id[word])
# convert to numpy array and append to main list
newlist.append(np.array(tmplist).astype(float)) # type float
print(newlist) # desired output: [array([ 2., 0., 1., 0.]), array([ 5., 3., 4., 6., 0.]), array([ 7., 0., 8., 0.])]
# and to see what id's represent which strings:
tdict[0] # 'a'