Python Lemmatizing输入列表,返回输出列表 [英] Python Lemmatizing input list, return output list

查看:115
本文介绍了Python Lemmatizing输入列表,返回输出列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含我要定格的字符串.尽管我可以对所有字符串进行去词素化,但是我仍然很难以与我输入到去词素化器中相同的列表格式返回去词素化的字符串.

I have a list containing strings that I am lemmatizing. Though I can lemmatize all the strings, I am having a hard time returning the lemmatized strings in the same list format that I inputted to the lemmatizer.

对每种输出进行某种类型的处理,我得到了一个unicode和str对象.我尝试将unicode转换为字符串,并尝试将字符串连接到列表,但没有运气.

Doing a type of each of the outputs, I get a unicode and str objects. I tried converting the unicode to strings and tried to concatenate the strings to a list but with no luck.

下面是可复制的代码:

typea = ['colors', 'caresses', 'ponies', 'presumably', 'owed', 'says']
for i in xrange(0,len(typea)):
    # Lemmatize the words
    lemmatized_words = lmtzr.lemmatize(typea[i])
    print lemmatized_words

    #Output obtained: 
    color
    caress
    pony
    presumably
    owed
    say
    #Desired output
    #['color', 'caress', 'pony', 'presumably', 'owed', 'say']

推荐答案

lmtzr.lemmatize采用单个字符串并返回单个字符串.因此lemmatized_words一次只能是一个字符串.

lmtzr.lemmatize takes a single string and returns a single string. So lemmatized_words will be a single string at a time.

要对所有单词进行词素化并将其存储在列表中,您需要这样的内容:

To lemmatize all the words and store them in a list, you want something like this:

typea = ['colors', 'caresses', 'ponies', 'presumably', 'owed', 'says']
lemmatized_words = [lmtzr.lemmatize(x) for x in typea]
print lemmatized_words

这篇关于Python Lemmatizing输入列表,返回输出列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆