简体   繁体   English

Python:随机选择替换多个单词

[英]Python: Replacing multiple words with a random choice

I have a phrase with some special marked words which I want to replace. 我有一个短语,上面有一些要替换的特殊标记词。 These words match a key in a dictionary, which has a list of words that I want to randomly choose to replace with. 这些单词与词典中的键匹配,词典中有我要随机选择替换的单词列表。

I'm wondering if there's a better way to go about doing this, or is what I have seem like a valid approach? 我想知道是否有更好的方法可以做到这一点,或者我认为这是一种有效的方法吗? I have a feeling there may be a smarter way with lambda but I'm not sure. 我觉得lambda可能有更聪明的方法,但我不确定。

Hopefully the code explains for itself! 希望代码能为自己解释!

import random

words = {"fruit":["apples", "bananas", "oranges"], 
         "veggies":["broccoli", "corn", "cucumbers"]}

txt = "I'm not in a mood for [veggies], I rather have [fruit], and [fruit]."

for key in words:
    target_word = "[{0}]".format(key)

    while target_word in txt:
        txt = txt.replace(target_word, random.choice(words[key]), 1)

Running it a few times will randomly output: 运行几次将随机输出:

I'm not in a mood for corn, I rather have bananas, and apples. 我不想吃玉米,我想吃香蕉和苹果。

I'm not in a mood for broccoli, I rather have oranges, and bananas. 我不想吃西兰花,我想吃橙子和香蕉。

I'm not in a mood for cucumbers, I rather have apples, and oranges. 我不想吃黄瓜,我想吃苹果和橙子。

..and so on.. ..等等..

I should mention that there could be any number of keys in words , and any number of marked words in the text. 我应该提到words可以有任意数量的键,文本中可以有任意数量的标记单词。

re.sub also accepts a callable as repl argument: re.sub还接受一个callable作为repl参数:

In [19]: import random, re
    ...: 
    ...: words = {"fruit":["apples", "bananas", "oranges"], 
    ...:          "veggies":["broccoli", "corn", "cucumbers"]}
    ...: 
    ...: txt = "I'm not in a mood for [veggies], I rather have [fruit], and [fruit]."
    ...: 

In [20]: regex = re.compile(r'\[({})\]'.format('|'.join(words)))

In [21]: regex.pattern
Out[21]: '\\[(fruit|veggies)\\]'

In [22]: regex.sub(lambda match: random.choice(words[match.group(1)]), txt)
Out[22]: "I'm not in a mood for broccoli, I rather have bananas, and apples."

In [23]: regex.sub(lambda match: random.choice(words[match.group(1)]), txt)
Out[23]: "I'm not in a mood for corn, I rather have oranges, and oranges."

I think that it's against Python Zen though. 我认为这与Python Zen相反。

i done it using re.findall then str.replace , but i feel its not really much better than yours either 我做了使用re.findall然后str.replace ,但我觉得它比你不是真的要好得多要么

import random

words = {"fruit":["apples", "bananas", "oranges"], 
         "veggies":["broccoli", "corn", "cucumbers"]}
txt = "I'm not in a mood for [veggies], I rather have [fruit], and [fruit]."

found = re.findall('\[\w+\]', txt)
for m in found:
    txt = txt.replace(m, random.choice(words.get(m.strip('[]'), [m])), 1)

This can be one way of doing it: 这可以是一种方法:

import random

words = {"fruit":["apples", "bananas", "oranges"], 
         "veggies":["broccoli", "corn", "cucumbers"]}

txt = "I'm not in a mood for [veggies], I rather have [fruit]."    
txt = txt.replace('[veggies]', random.choice(words['veggies'])).replace('[fruit]', random.choice(words['fruit']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM