[英]How to replace multiple strings in python?
我正在尝试替换列表中的多个字符串,但我没有达到。 我要替换的字符串是。 数字代表单词的正确顺序
sentence = ['1', '2', '3', '4']
我想用文本 'i', 'put', 'this', 'here' 替换数字,使其如下所示
['i', 'put', 'this', 'here']
我发现一行代码只能替换一个单词。
newsentence = [n.replace('1', 'I') for n in sentence]
我试图重复代码 4 次,以便它替换所有数字。
newsentence = [n.replace('1', 'I') for n in sentence]
newsentence = [n.replace('2', 'put') for n in sentence]
newsentence = [n.replace('3', 'this') for n in sentence]
newsentence = [n.replace('4', 'here') for n in sentence]
但结果是执行了最后一次替换,导致
['1', '2', '3', 'here']
感谢您的任何反馈
请参阅@KeyurPotdar 的答案,以解释您的原始解决方案为何不起作用。
对于您的问题的基于列表理解的解决方案(这似乎是您所追求的),您可以创建输入到输出的映射,然后使用您的输入迭代映射
mapping = {
'1': 'i',
'2': 'put',
'3': 'this',
'4': 'here',
}
sentence = ['1', '2', '3', '4']
newsentence = [mapping[word] for word in sentence]
# ['I', 'put', 'this', 'here']
这很好,但是如果您决定在尚未定义输出的mapping
抛出更多输入,则会得到KeyError
。 为了轻松处理这个问题,您可以使用dict.get
,它允许您提供一个回退值,如果给定的键丢失,则返回。
newsentence = [mapping.get(word, word) for word in sentence]
# ['I', 'put', 'this', 'here']
在这种情况下,不仅基于映射的解决方案更有效(请参阅 @KeyurPotdar 对此的注释),而且将代码分离为数据和逻辑是正确的事情™。
如果您可以将基于代码/逻辑的问题(例如原始问题中的列表推导式序列)转换为基于映射的问题,您几乎总能在可维护性和代码清晰度方面获胜。 观察这个解决方案的数据和逻辑是混合的:
newsentence = [n.replace('1', 'I') for n in sentence]
newsentence = [n.replace('2', 'put') for n in newsentence]
newsentence = [n.replace('3', 'this') for n in newsentence]
newsentence = [n.replace('4', 'here') for n in newsentence]
但是,在基于映射的解决方案中,它们是分开的
# DATA
mapping = {
'1': 'i',
'2': 'put',
'3': 'this',
'4': 'here',
}
# LOGIC
newsentence = [mapping.get(word, word) for word in sentence]
这给你买什么? 假设您最终不得不支持映射 1000 个单词,并且这些单词经常更改。 将单词与逻辑混合在一起会使它们更难找到,并且如果更改只会影响数据或还可能意外更改控制流,则更难在精神上解耦。 对于基于映射的解决方案,肯定的是更改只会影响数据。
考虑到我们需要添加'1a'
到'we'
的映射。 在混合逻辑/数据示例中,很容易错过将sentence
更改为newsentence
:
newsentence = [n.replace('1', 'I') for n in sentence]
newsentence = [n.replace('1a', 'we') for n in sentence]
newsentence = [n.replace('2', 'put') for n in newsentence]
newsentence = [n.replace('3', 'this') for n in newsentence]
newsentence = [n.replace('4', 'here') for n in newsentence]
哎呀! 在基于映射的示例中,这种类型的错误在设计上是不可能的。
此外,通过将数据与逻辑解耦,可以开始将数据存储在单独的文件(例如 JSON 或 YAML)中。 这使得版本控制更简单一些。 然后它开启了拥有用户可自定义映射的可能性,您不必将这些映射硬编码到脚本中。 解耦==好。
看起来您知道需要替换哪些单词。 使用以下方法将在O(n)
时间内做到这一点。
changes = {
'1': 'i',
'2': 'put',
'3': 'this',
'4': 'here'
}
sentence = ['1', '2', '3', '4']
newsentence = []
for word in sentence:
try:
newsentence.append(changes[word])
except KeyError:
newsentence.append(word)
print(newsentence)
# ['i', 'put', 'this', 'here']
说明:
您在这里要做的是检查列表项是否在字典中可用。 如果可用,则使用它的值并将该值附加到新列表中。 否则,直接追加旧列表的值。
代码changes[word]
在该行newsentence.append(changes[word])
将引发KeyError
,如果关键是不是在字典可用。 这就是为什么我们要捕获该错误并直接附加单词的原因。
另请注意,此代码使用EAFP 原理。
注意:下面的代码只是为了告诉你哪里出错了。 避免使用它(原因如下所述)。
为了帮助您了解代码中发生的事情,请查看以下代码段:
>>> sentence = ['1', '2', '3', '4']
>>> newsentence = [n.replace('1', 'I') for n in sentence]
>>> newsentence
['I', '2', '3', '4']
>>> newsentence = [n.replace('2', 'put') for n in sentence]
>>> newsentence
['1', 'put', '3', '4']
>>> newsentence = [n.replace('3', 'this') for n in sentence]
>>> newsentence
['1', '2', 'this', '4']
...
简而言之,您每次都替换原始句子中的单个项目,即['1', '2', '3', '4']
。 要更换所有项目替换sentence
与newsentence
第一替换行之后。
sentence = ['1', '2', '3', '4']
newsentence = [n.replace('1', 'I') for n in sentence]
newsentence = [n.replace('2', 'put') for n in newsentence]
newsentence = [n.replace('3', 'this') for n in newsentence]
newsentence = [n.replace('4', 'here') for n in newsentence]
print(newsentence)
# ['I', 'put', 'this', 'here']
但是,避免使用此代码,因为成本是二次的; 或者更准确地说,它是O(m*n)
,其中m
是列表的大小, n
是要替换的单词数; 显然,如果列表更大,写这个是不可行的。
你可以这样做:
newsentence = [n.replace('1', 'I') for n in sentence]
newsentence = [n.replace('2', 'put') for n in newsentence]
newsentence = [n.replace('3', 'this') for n in newsentence]
newsentence = [n.replace('4', 'here') for n in newsentence]
这意味着每次都会更改前一个,而不是恢复到原始状态,然后只进行一次替换。 您还可以使用format()
:
sentence='{1}{2}{3}{4}{5}'
sentence.format('I', 'put', 'this', 'here')
有关format()
函数的更多信息,请参阅此页面。
有一种翻译方法,您可以通过该方法进行多次替换
s = '1 2 3 4'
replaced_string = s.translate(str.maketrans({'1': 'I', '2': 'put', '3': 'this', '4': 'here'}))
print(replaced_string)
#output: I put this here
您可以通过这种方式避免嵌套的替换语句干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.