【问题标题】:How to replace specific words from string如何替换字符串中的特定单词
【发布时间】:2021-10-02 06:11:42
【问题描述】:

我正在尝试在 python 字符串中进行一些替换,我有类似的数据 The lyrics is not that bad! 我想用 good 替换 not that bad。这是我拥有的几个数据示例:

I HAVE THIS  --  WANT TO CONVERT LIKE THIS
The lyrics is not that bad!  --  The lyrics is good!
Food is not bad.  --  Food is good.
!! not !! bad !!  --  !! good !!
notbad  --  good
The song is not extremely gently bad for my ears.  --  The song is good for my ears.
The sight is not very very bad.  -- The sight is good.

我正在尝试编写一些适用于所有人的通用脚本。我是python的新手,我试过str.replace(),找到str.find()然后替换。但没有成功。

【问题讨论】:

  • str.replace??
  • 到目前为止你有什么尝试?
  • 所以我希望它适用于不同的句子。喜欢:- 输入是“歌词还不错!”输出是“歌词很好!”和输入 - 视力不是很差。输出 - 视线很好。我怎样才能把它变成一个程序?........我试过 str.replace,我也试过 str.find 然后替换,但没有任何效果。

标签: python python-3.x replace


【解决方案1】:

你可以试试这个:

string=input("Enter string")
#Input from user is :
string="The lyrics is not that bad!"
string=string.replace("not that bad","good")
print(string)
# The lyrics is good!

这里我们要求用户输入字符串,用户输入 The lyrics is not that bad! 现在我们必须将 not that bad 替换为 goodstring.replace("not that bad","good")
编辑:
如果你想概括,我在你的字符串中找到了一些模式。就像您想用“好”替换“不”和“坏”之间的文本。所以,你可以试试这个:

import re

string=input("Enter string")

#Input from user is :
string="The sight is not very very bad."
string="Food is not bad."
string="The song is not extremely gently bad for my ears."

string=re.sub("not(.*?)bad","good",string)

print(string)
# The sight is good.
# Food is good.
# The song is good for my ears.

【讨论】:

  • 它适用于这个例子......但不适用于其他例子,如:- 输入 - 视力不是很差。输出 - 视线很好。这就是我想概括它的原因。
  • 你必须说出这个问题!好的!我也会编辑我的答案。
  • @SohamSharma 最后的编辑回答了你的问题吗?
  • 我真的不擅长这个!请原谅我!所以这些是我需要的结果:- inp = '''歌词还不错!''' out = '''歌词不错!''' inp = '''食物还不错。''' out = '''食物很好。''' inp = '''!!不是 !!糟糕!!'''出='''!! good !!''' inp = '''notbad''' out = '''good''' inp = '''这首歌不是很温柔''' out = '''这首歌对我的耳朵很好。''' inp = '''视力还不错。''' out = '''视力很好。' '' 代码应该适用于这些条件。
  • 您的efforts in helping the OP to ask better 是“以身作则”。我看到了您的编辑,将所有 OP 的澄清 cmets 添加到问题中。
【解决方案2】:

要创建更强大的解决方案,您需要像机器人一样进行设计,但对于您当前的问题,我可以建议一些通用解决方案。看看下面的例子。

# have a list of common terms you want to replace
terms = ["not that bad", "not very bad", "not very very bad"]

# take a user input sentence
str1=input("Enter sentence - ")

# iterate on terms and check if term is present in sentence, if there replace and break
for term in terms:
    if term in str1:
        reverse = str1.replace(term, "good")
        break

# print replaced string
print(reverse)

【讨论】:

  • 这些是我需要的结果:- inp = '''歌词还不错!''' out = '''歌词很好!''' inp = '''Food还不错。''' out = '''食物很好。''' inp = '''!!不是 !!糟糕!!'''出='''!! good !!''' inp = '''notbad''' out = '''good''' inp = '''这首歌对我的耳朵不是特别轻。''' out = '''这首歌对我的耳朵有好处。''' inp = '''视力不是很差。''' out = '''视力很好。''' 代码应该适用于这些条件。就像我想将它概括为任何条件。
【解决方案3】:

要概括某件事,需要有一个清晰的模式。您所有的输入都太不同了,无法将它们过滤掉。 我会使用具有以下结构的字典:

input_correction = {"not that bad": "good",
                    "not bad": "good",
                    "not very very bad": "good"}

for key, replace_str in input_correction.items():
    if key in inp:
        inp.replace(key, replace_str)

只需涵盖您的 dict 中的每个案例和您的好去处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2021-06-30
    • 2021-08-20
    相关资源
    最近更新 更多