用Python将句子中在列表中出现的单词替换为特定单词。

huangapple go评论69阅读模式
英文:

Replace words in a sentence with a specific word if they occur in a list in python

问题

我有一个作为字典的映射:

{
    'a':['a1','a2','a3'],
    'b':['b1','b2'],
    'c':['c1','c2','c3','c4']
}

给定一个句子,所有出现的'a1','a2','a3'都应该被替换为'a',类似地,所有出现的'b1','b2'都应该被替换为'b'。如何高效地完成这个任务?这是当前的代码:

def word_replacer(text):
    s = {
        'a'['a1''a2''a3']
        'b'['b1''b2']
        'c'['c1''c2''c3''c4']
    }
    words = text.split(" ")
    for idx, word in enumerate(words):
        changed_word=[k for k, v in s.items() if word in [vals.lower() for vals in v]]
        if ((len(changed_word)>0) and (word != changed_word[0])):
            words[idx] = changed_word[0]
    result = " ".join(words)
    return result

你可以将这段代码修改为以下内容以提高效率:

def word_replacer(text):
    s = {
        'a'['a1''a2''a3']
        'b'['b1''b2']
        'c'['c1''c2''c3''c4']
    }
    words = text.split(" ")
    for idx, word in enumerate(words):
        for key, values in s.items():
            if word.lower() in values:
                words[idx] = key
                break
    result = " ".join(words)
    return result

这个修改后的代码将更高效地执行相同的替换操作。

英文:

I have a mapping as a dict

{
'a': ['a1','a2','a3'],
'b': ['b1', 'b2'],
'c': ['c1', 'c2', 'c3', 'c4]
}

Given a sentence, all occurrences of 'a1', 'a2', 'a3' should be replaced with 'a', similarly all occurrences of 'b1', 'b2' should be replaced with 'b'. How can this be done efficiently? This is the current code:

def word_replacer(text):
    s = {
         'a': ['a1','a2','a3'],
         'b': ['b1', 'b2'],
         'c': ['c1', 'c2', 'c3', 'c4']
        }
    words = text.split(" ")
    for idx, word in enumerate(words):
        changed_word=[k for k, v in s.items() if word in [vals.lower() for vals in v]]
        if ((len(changed_word)>0) and (word != changed_word[0])):
            words[idx] = changed_word[0]
    result = " ".join(words)
    return result

答案1

得分: 2

以下是翻译好的内容:

代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。

英文:

It would be easier to just reverse the dict and make a quick single look-up. If you can, just change it manually. If it's an input:

mapping = {v: k for k, l in s.items() for v in l}

And now the code is much simpler:

def word_replacer(text):
    s = {
         'a': ['a1','a2','a3'],
         'b': ['b1', 'b2'],
         'c': ['c1', 'c2', 'c3', 'c4']
        }
    mapping = {v: k for k, l in s.items() for v in l}

    words = text.split()
    for idx, word in enumerate(words):
        words[idx] = mapping[word]
    result = " ".join(words)
    return result

Or even simpler using the get method of dicts:

def word_replacer(text):
    s = {
         'a': ['a1','a2','a3'],
         'b': ['b1', 'b2'],
         'c': ['c1', 'c2', 'c3', 'c4']
        }
    mapping = {v: k for k, l in s.items() for v in l}

    return " ".join(mapping.get(word, word) for word in text.split())

答案2

得分: 1

一个高效的方法是创建一个将别名映射到对应单词的字典这样你可以使用`dict.get`方法以常数时间查找每个单词并将其替换为映射的值如果找到):

s = {
    'a': ['a1', 'a2', 'a3'],
    'b': ['b1', 'b2'],
    'c': ['c1', 'c2', 'c3', 'c4']
}
mapping = {alias: word for word, aliases in s.items() for alias in aliases}

def word_replacer(text):
    return ' '.join(mapping.get(word, word) for word in text.split())

所以

print(word_replacer('a1 b2 c3 d4'))

输出

a b c d4
英文:

An efficient approach would be to create a dict that maps aliases to the mapped words, so that you can use the dict.get method to look up each word in constant time and replace it with the mapped value if found:

s = {
    'a': ['a1', 'a2', 'a3'],
    'b': ['b1', 'b2'],
    'c': ['c1', 'c2', 'c3', 'c4']
}
mapping = {alias: word for word, aliases in s.items() for alias in aliases}

def word_replacer(text):
    return ' '.join(mapping.get(word, word) for word in text.split())

so that:

print(word_replacer('a1 b2 c3 d4'))

outputs:

a b c d4

huangapple
  • 本文由 发表于 2023年1月9日 15:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定