简体   繁体   中英

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']
brownDT = [(i,(a,b)) for (i, (a,b)) in enumerate(brown_tag) if b == 'DT']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM