如何替换python中句子中出现一次的单词

Mar*_*ars -1 python

我想用一句话替换出现一次的单词'<unk>'.喜欢一句话:hello hello world my world,我想要输出hello hello world <unk> world,怎么做?

现在我这样做:

 wordlist1 = trainfiles.split(None)
        wordlist2 = []
        for word1 in wordlist1:
            lastchar = word1[-1:]
            if lastchar in [",",".","!","?",";"]:
                word2 = word1.rstrip(lastchar)
            else:
                word2 = word1
            wordlist2.append(word2)
        freq = {}
        for word2 in wordlist2:
            freq[word2] = freq.get(word2,0)+1
        keylist = freq.keys()
        keylist.sort()

    for key2 in keylist:
        if freq[key2] == 1:
            print "%-10s %d" % ('<unk>', freq[key2])
        else:
            print "%-10s %d" % (key2, freq[key2])
Run Code Online (Sandbox Code Playgroud)

这给了我一个输出:

hello   2
<unk>   1
world   2
Run Code Online (Sandbox Code Playgroud)

但是,我需要输出如下:

hello hello world <unk> world
Run Code Online (Sandbox Code Playgroud)

怎么做?

Cor*_*mer 5

使用collections.Counter计数频率的话在你的句子

from collections import Counter
s = 'hello hello world my world'
counts = Counter(s.split())
Run Code Online (Sandbox Code Playgroud)

然后使用生成器表达式替换任何计数为1的单词,并将结果与​​空格字符连接.

replaced = ' '.join(i if counts[i] > 1 else '<unk>' for i in s.split())
Run Code Online (Sandbox Code Playgroud)

结果

'hello hello world <unk> world'
Run Code Online (Sandbox Code Playgroud)