Reputation: 167
I have the following:
[('The', 'NNP'), ('map', 'NNP'), ('is', 'VBZ'),
('way', 'NN'), ('of', 'IN'), ('using', 'VBG'), ('tool', 'NN'),
(',', ','), ('skill', 'VBG'), ('and', 'CC')]
And, I need to replace the string with 'NN' every time it's paired with 'NN'. So the output should be:
[('The', 'NNP'), ('map', 'NNP'), ('is', 'VBZ'),
('NN', 'NN'), ('of', 'IN'), ('using', 'VBG'), ('NN', 'NN'),
(',', ','), ('skill', 'VBG'), ('and', 'CC')]
I tried the following, but it won't replace the item. please help!
for s in sentence:
if s[1] == 'NN':
s[0] == 'NN'
print(sentence)
Upvotes: 1
Views: 73
Reputation: 402814
Tuples cannot be assigned to - they're immutable. One option is to re-build your POS tags list from scratch.
pos_tags = [(x if y != 'NN' else y, y) for x, y in pos_tags]
If not this, then, re-assign your list items.
for i, (x, y) in enumerate(pos_tags):
if y == 'NN':
pos_tags[i] = ('NN', 'NN')
print(pos_tags)
[('A', 'NNP'),
('Discourse', 'NNP'),
('is', 'VBZ'),
('NN', 'NN'),
('of', 'IN'),
('using', 'VBG'),
('NN', 'NN'),
(',', ','),
('thinking', 'VBG'),
('and', 'CC'),
('acting', 'VBG')]
Upvotes: 2