Python Lemmatizing input list, return output list

依然范特西╮ 提交于 2019-12-24 09:37:15

问题


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.

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.

Below is the reproducible code:

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']

回答1:


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


来源:https://stackoverflow.com/questions/43747659/python-lemmatizing-input-list-return-output-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!