Skip to content
Advertisement

Getting a specific element from a list of tuples

In using Spacy, I have the following:

import spacy

nlp = spacy.load('en_core_web_lg')

sentence = "a quick John jumps over the lazy dog"

tag_entities = [(x, x.ent_iob_, x.ent_type_) for x in nlp(sentence)]
inputlist = tag_entities

print (inputlist)

[(a, 'O', ''), (quick, 'O', ''), (John, 'B', 'PERSON'), (jumps, 'O', ''), (over, 'O', ''), (the, 'O', ''), (lazy, 'O', ''), (dog, 'O', '')]

It is a list of tuples. I want to extract the person element. This is what I do:

for i in inputlist:
  if (i)[2] == "PERSON":
    print ((i)[0])

John

What would be a better way?

Advertisement

Answer

To keep all first element if second element is PERSON from first list use a list comprehension notation with a if at the end

filtered_taglist = [x for x,_,type in tag_entities if type == "PERSON"]

This corresponds to

filtered_taglist = []
for x,_,type in inputlist:
    if type == "PERSON":
        filtered_taglist.append(x)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement