Python 如何使用Scikit将单词映射到从index=1开始的索引

Python 如何使用Scikit将单词映射到从index=1开始的索引,python,mapping,nlp,scikit-learn,vocabulary,Python,Mapping,Nlp,Scikit Learn,Vocabulary,有没有一种方法可以使用Scikit学习将单词映射到从1开始而不是从0开始的索引 示例-伪代码: sequence = ['welcome', 'home', 'shimon'] dict = mapping_func(sequence) print(dict['welcome']) print(dict['home']) print(dict['shimon']) 而此代码的输出为: 一, 二, 三, 我需要这个选项来设置填充序列的零,如果值0属于某个键,则可能(也可能会)导致错误的结果。如

有没有一种方法可以使用Scikit学习将单词映射到从1开始而不是从0开始的索引

示例-伪代码

sequence = ['welcome', 'home', 'shimon']
dict = mapping_func(sequence)

print(dict['welcome'])
print(dict['home'])
print(dict['shimon'])
而此代码的输出为:

一,

二,

三,


我需要这个选项来设置填充序列的零,如果值0属于某个键,则可能(也可能会)导致错误的结果。

如果您有一个单词列表,如
sequence=['welcome'、'home'、'shimon']
并将其填充为0,则将得到
sequence=['welcome'、'home'、'shimon',0,0]
。然后您可以始终执行l.index(
欢迎
)来检索索引。如果您对一个单词有多个索引的情况感兴趣,可以使用列表理解

>>>sequence= ['welcome', 'home', 'shimon', 0, 0]
>>>indices = [i for i, x in enumerate(sequence) if x == 0]
>>>indices
[3,4]
>>>indices = [i for i, x in enumerate(sequence) if x == 'welcome']
>>>indices
[0]

我不明白为什么它会导致错误的结果?Python数组、列表等都是0索引。如果你解释得更多,我们可以告诉你,0索引将不会成为一个问题。我一般知道如何做。我问的是如何使用Scikit,尤其是CountVectorizer