Python替换函数【替换一次】 [英] Python replace function [replace once]

查看:78
本文介绍了Python替换函数【替换一次】的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要关于我正在用 Python 编写的程序的帮助.

假设我想将单词 "steak" 的每个实例替换为 "ghost"(只需使用它...)但我也想替换每个"ghost""steak" 的实例.以下代码不起作用:

 s="可怕的幽灵点了一份昂贵的牛排"印刷s=s.replace("牛排","幽灵")s=s.replace("幽灵","牛排")印刷

它打印:可怕的牛排订购了昂贵的牛排

我想要的是可怕的牛排点了一个昂贵的幽灵

解决方案

我可能会在这里使用正则表达式:

<预><代码>>>>进口重新>>>s = "可怕的鬼点了一份昂贵的牛排">>>sub_dict = {'ghost':'steak','steak':'ghost'}>>>正则表达式 = '|'.join(sub_dict)>>>re.sub(regex, lambda m: sub_dict[m.group()], s)可怕的牛排点了一个昂贵的鬼魂"

或者,作为一个可以复制/粘贴的函数:

导入重新def word_replace(replace_dict,s):正则表达式 = '|'.join(replace_dict)返回 re.sub(regex, lambda m: replace_dict[m.group()], s)

基本上,我创建了一个我想用其他词替换的词的映射 (sub_dict).我可以从该映射创建一个正则表达式.在这种情况下,正则表达式是 "steak|ghost"(或 "ghost|steak" -- 顺序无关紧要),正则表达式引擎会完成剩下的工作寻找非重叠序列并相应地替换它们的工作.

<小时>

一些可能有用的修改

  • regex = '|'.join(map(re.escape,replace_dict)) -- 允许正则表达式中包含特殊的正则表达式语法(如括号).这会转义特殊字符以使正则表达式与文字文本匹配.
  • regex = '|'.join(r'\b{0}\b'.format(x) for x in replace_dict) -- 确保我们不匹配,如果有我们的词是另一个词的子串.换句话说,将 he 改为 she 而不是 the 改为 tshe.

I need help with a program I'm making in Python.

Assume I wanted to replace every instance of the word "steak" to "ghost" (just go with it...) but I also wanted to replace every instance of the word "ghost" to "steak" at the same time. The following code does not work:

 s="The scary ghost ordered an expensive steak"
 print s
 s=s.replace("steak","ghost")
 s=s.replace("ghost","steak")
 print s

it prints: The scary steak ordered an expensive steak

What I'm trying to get is The scary steak ordered an expensive ghost

解决方案

I'd probably use a regex here:

>>> import re
>>> s = "The scary ghost ordered an expensive steak"
>>> sub_dict = {'ghost':'steak','steak':'ghost'}
>>> regex = '|'.join(sub_dict)
>>> re.sub(regex, lambda m: sub_dict[m.group()], s)
'The scary steak ordered an expensive ghost'

Or, as a function which you can copy/paste:

import re
def word_replace(replace_dict,s):
    regex = '|'.join(replace_dict)
    return re.sub(regex, lambda m: replace_dict[m.group()], s)

Basically, I create a mapping of words that I want to replace with other words (sub_dict). I can create a regular expression from that mapping. In this case, the regular expression is "steak|ghost" (or "ghost|steak" -- order doesn't matter) and the regex engine does the rest of the work of finding non-overlapping sequences and replacing them accordingly.


Some possibly useful modifications

  • regex = '|'.join(map(re.escape,replace_dict)) -- Allows the regular expressions to have special regular expression syntax in them (like parenthesis). This escapes the special characters to make the regular expressions match the literal text.
  • regex = '|'.join(r'\b{0}\b'.format(x) for x in replace_dict) -- make sure that we don't match if one of our words is a substring in another word. In other words, change he to she but not the to tshe.

这篇关于Python替换函数【替换一次】的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆