Irfan
Irfan

Reputation: 99

convert a function outputting string new line by new line to list

i have a function that is generating string elements using a for loop like this:

a
b
c
d
....

how can i convert my ouput to list like:

['a','b','c','d'...] 

and so on..

the function is as follows:

def proper_lemmatize_sentence(sent, cond):
    tokens = word_tokenize(sent)
    tagged_tokens = pos_tag(tokens)
    for tagged_token in tagged_tokens:
        word = tagged_token[0]
        if cond:
            word_pos = tagged_token[1]
        else:
            word_pos = 'n'
        word_pos1 = get_wordnet_pos(word_pos)
        lemma = wnl.lemmatize(word, pos=word_pos1)
        print(lemma)

Upvotes: 0

Views: 41

Answers (2)

Diego Peinador
Diego Peinador

Reputation: 81

Try with:

text = text.split('\n')

Upvotes: 0

wwii
wwii

Reputation: 23773

Create the list before the loop; append to the list at the bottom of the loop; then return (or print) the list when the loop completes.

s = 'abcde'
def f(sequence):
    result = []
    for lemma in sequence:
        # do stuff
        result.append(lemma)
    return result

>>> print(f(s))
['a', 'b', 'c', 'd', 'e']
>>> 

Upvotes: 1

Related Questions