列表中有许多重复的元组

Many Repeated Tuples in list

我无法处理列表中的元组。 假设我们有一个包含很多元组的列表。

simpleTag=[**('samsung', 'ADJ')**, ('user', 'NOUN'), ('huh', 'NOUN'), ('weird', 'NOUN'), (':', '.'), ('MDPai05', 'NOUN'), (':', '.'), ('Samsung', 'NOUN'), ('Electronics', 'NOUN'), ('to', 'PRT'), ('Build', 'NOUN'), ('$', '.'), ('3', 'NUM'), ('Billion', 'NUM'), ('Smartphone', 'NOUN'), ('Plant', 'NOUN'), ('in', 'ADP'), ('Vietnam', 'NOUN'), ('Why', 'NOUN'), ('not', 'ADV'), ('india', 'VERB'), ('?', '.'), ('market', 'NOUN'), ('here', 'ADV'), (':', '.'), (':', '.'), ('//t…I', 'ADJ'), ('have', 'VERB'), ('bricked', 'VERB'), ('an', 'DET'), ('android', 'ADJ'), ('samsung', 'NOUN'), ('galaxy', 'NOUN'), ('player', 'NOUN'), ('yp-g70', 'X'), ('international', 'ADJ'), ('version', 'NOUN'), (',', '.'), ('and', 'CONJ'), ('it', 'PRON'), ('is', 'VERB'), ("n't", 'ADV'), ('recognized', 'VERB'), ('by', 'ADP'), ('PC', 'NOUN'), ('an', 'DET'), ('...', '.'), (':', '.'), ('tomwicky', 'NOUN'), (':', '.'), (':', '.'), ('announces', 'NOUN'), ('partnership', 'NOUN'), ('with', 'ADP'), ('Samsung', 'NOUN'), ('for', 'ADP'), ('wallet', 'NOUN'), ('/', '.'), ('couponing', 'VERB'), ('oms14', 'NOUN'), (':', '.'), ('refrigerator', 'NOUN'), ('(', '.'), ('Spearfish', 'ADJ'), (')', 'NOUN'), ('$', '.'), ('175', 'NUM'), (':', '.'), ('refrigerator', 'NOUN'), ('samsung', 'NOUN'), ('airconditioning', 'VERB'), ('sd', 'NOUN'), ('forsale', 'NOUN'), (':', '.'), ('relaxedharry', 'NOUN'), (':', '.'), ('meanwhile', 'ADV'), ('louis', 'VERB'), ('is', 'VERB'), ('a', 'DET'), **('samsung', 'ADJ')**, ('user', 'NOUN'), ('huh', 'NOUN'), ('weird', 'NOUN'), (':', '.'), ('AmazingRoom', 'NOUN'), (':', '.'), ('if', 'ADP'), ('you', 'PRON'), ('want', 'VERB'), ('a', 'DET'), ('iPhone', 'NOUN'), ('5s', 'NUM'), ('!', '.'), ('*', 'X'), (':', '.'), ('to', 'PRT'), ('win', 'VERB'), ('a', 'DET'), ('Samsung', 'NOUN')]

我想做的是替换一些元组的值。例如在simpleTag中,有"samsung"有两个标签:'NOUN'和'ADJ'

我想做的是将 'ADJ' 替换为“NOUN”

我尝试了一些代码,但我不知道为什么 returns 我 "ADJ." 上面的例子只是我用 nltk 标记的代码示例。 请告诉我你的想法。

代码 1:

[tupleset[:1] + ('NOUN',) for tupleset in simpleTag if word.startswith('samsung')]

代码 2:

for (word,tag) in simpleTag:
    if word.startswith('samsung'):
        tag = 'NOUN'

代码 3:

for (word,tag) in simpleTag:
    if word.startswith('samsung'):
        (word, tag)=(word, 'NOUN')

这是给定代码的简单修改:

for index, (word,tag) in enumerate(simpleTag):
    if word.startswith('samsung'):
        simpleTag[index] = (word, 'NOUN')

看来你把它复杂化了。在 Python 中,通常更有效且更容易理解列表而不是改变它

[(word, 'NOUN' if word.startswith('samsung') else tag)
 for word, tag in simpleTag]