I have two lists originating from a part of speech tagger which look as follows:
pos_tags = [('This', u'DT'), ('is', u'VBZ'), ('a', u'DT'), ('test', u'NN'), ('sentence', u'NN'), ('.', u'.'), ('My', u"''"), ('name', u'NN'), ('is', u'VBZ'), ('John', u'NNP'), ('Murphy', u'NNP'), ('and', u'CC'), ('I', u'PRP'), ('live', u'VBP'), ('happily', u'RB'), ('on', u'IN'), ('Planet', u'JJ'), ('Earth', u'JJ'), ('!', u'.')]
pos_names = [('John', 'NNP'), ('Murphy', 'NNP')]
I want to create a final list which updates pos_tags with the list items in pos_names. So basically I need to find John and Murphy in pos_tags and replace the POS tag with NNP.
I agree a dictionary would be a more natural solution to this problem, but if you need your
pos_tags
in order a more explicit solution would be:(A dictionary would probaby be faster for a large number of words, so you might want to consider storing the word order in a list and doing your POS allocation using a dictionary).