python - How do I select the first elements of each list in a list of lists? -
i trying isolate first words in series of sentences using python/ nltk.
created unimportant series of sentences (the_text) , while able divide tokenized sentences, cannot separate first words of each sentence list (first_words).
[['here', 'is', 'some', 'text', '.'], ['there', 'is', 'a', 'a', 'person', 'on', 'the', 'lawn', '.'], ['i', 'am', 'confused', '.'], ['there', 'is', 'more', '.'], ['here', 'is', 'some', 'more', '.'], ['i', 'do', "n't", 'know', 'anything', '.'], ['i', 'should', 'add', 'more', '.'], ['look', ',', 'here', 'is', 'more', 'text', '.'], ['how', 'great', 'is', 'that', '?']]
the_text="here text. there a person on lawn. confused. " the_text= (the_text + "there more. here more. don't know anything. ") the_text= (the_text + "i should add more. look, here more text. how great that?") sents_tok=nltk.sent_tokenize(the_text) sents_words=[nltk.word_tokenize(sent) sent in sents_tok] number_sents=len(sents_words) print (number_sents) print(sents_words) in sents_words: first_words=[] first_words.append(sents_words (i,0)) print(first_words) thanks help!
there 3 problems code, , have fix 3 make work:
for in sents_words: first_words=[] first_words.append(sents_words (i,0)) first, you're erasing first_words each time through loop: move first_words=[] outside loop.
second, you're mixing function calling syntax (parentheses) indexing syntax (brackets): want sents_words[i][0].
third, for in sents_words: iterates on elements of sents_words, not indices. want i[0]. (or, alternatively, for in range(len(sents_words)), there's no reason that.)
so, putting together:
first_words=[] in sents_words: first_words.append(i[0]) if know comprehensions, may recognize pattern (start empty list, iterate on something, appending expression list) list comprehension does:
first_words = [i[0] in sents_words] if don't, either time learn comprehensions, or don't worry part. :)
Comments
Post a Comment