Search code examples
pythonnlpnltkstanford-nlprasa-nlu

How to get the index of value with list comprehension


I am working in Brown Corpus using NLTK. I want to separate out the tokens that has tokens tagged with DT

My code:

import nltk
from nltk.corpus import brown
brown_tag = brown.tagged_words()
brownDT = [(a,b) for (a,b) in brown_tag if b == 'DT']

The above code returns the value tagged with DT but I need the index too. I am trying to get the value and the index of the value in return. For example the output should be:

[index, (token, 'DT')]

This code does not work:

brownDT = [((a,b),brown_tag.index((a,b))) for (a,b) in brown_tag if b == 'DT']

Solution

  • brownDT = [(i,(a,b)) for (i, (a,b)) in enumerate(brown_tag) if b == 'DT']