python - NLTK index of a word with mulitiple occurences -
i'm trying use python find index of word 'the' in following text
sent3 = ['in', 'the', 'beginning', 'god', 'created', 'the', 'heaven', 'and', 'the', 'earth', '.']
if sent3.index('the')
1
, index of first occurrence of word. i'm not sure on how find indexes of other times "the" appears. know how go doing this?
thanks!
[i i, item in enumerate(sent3) if item == wanted_item]
demo:
>>> sent3 = ['in', 'the', 'beginning', 'god', 'created', 'the', 'heaven', 'and', 'the', 'earth', '.'] >>> [i i, item in enumerate(sent3) if item == 'the'] [1, 5, 8]
enumerate
constructs list
of tuples iterable, consisting of values , corresponding indices. can use check if value want, , if pull index it.
Comments
Post a Comment