我的元组下面的列表:如何更改元组列表的值?
lis = [('The', 'DET'),
('iphone', 'X'),
('is', 'VERB'),
('a', 'DET'),
('very', 'ADV'),
('nice', 'ADJ'),
('device', 'NOUN'),
('.', 'PUNCT'),
('However', 'ADP'),
('the', 'DET'),
('pixel', 'X'),
('is', 'VERB'),
('by', 'ADP'),
('far', 'ADV'),
('more', 'ADV'),
('interesting', 'ADJ'),
('since', 'ADV'),
('it', 'PRON'),
('was', 'AUX'),
('made', 'VERB'),
('by', 'ADP'),
('google', 'NOUN'),
('.', 'PUNCT')]
我的主要目标是明确改变的元组的值:('iphone', 'X'), ('pixel', 'X'), ('google', 'NOUN')
到('iphone', 'device'), ('pixel', 'device'), ('google', 'entity')
。因此,由于我感兴趣的保存顺序,我试过如下:
tags['Google'] = 'device'
tags['pixel'] = 'device'
tags['iphone'] = 'entity'
#this one is not present in lis . Nevertheless, I would like to add it just in case I need it.
tags['galaxy'] = 'device'
tags = list(tags.items())
tags = OrderedDict(postag(str(sample)))
自从我加入tags['galaxy'] = 'device'
它实际上是在列表为('galaxy', 'device')
的末尾添加它。因此,我的问题是如何修复和更新元组的值,如果它们存在?
是否有必要使用元组列表?元组是不可改变的,所以设计我不会使用它们,如果我知道它们可能会改变。例如,也许你可以使用字典来存储映射,也可以使用单独的列表来保存键的顺序? – MxyL
@MxyL不幸的是,元组是必要的...我也想出你的想法。 –