【发布时间】:2020-08-18 14:45:28
【问题描述】:
我有一个相当具体的编程问题已经困扰了我几个星期了,我相信这是正确的地方。
我需要一个函数,它可以生成各种子字符串的所有组合,例如,替换为更大的字符串;
thisfunction([['Hello','hi'],['Goodbye','bye'],['Hello','goodbye']], "This is a string that says Hello and Goodbye and Hello again")
应该返回
["String that says Hello, Goodbye and Hello again",
"String that says hi and Goodbye and Hello again",
"String that says hi and bye and Hello again",
"String that says Hello and bye and Hello again",
"String that says Hello, Goodbye and goodbye again",
"String that says hi and Goodbye and goodbye again",
"String that says hi and bye and goodbye again",
"String that says Hello and bye and goodbye again",]
我用正则表达式和替换函数尝试了各种策略,但没有成功
感谢所有想法!
【问题讨论】:
-
您的列表
['Hello','hi'],['Goodbye','bye'],['Hello','goodbye']]包含'Hello'两次;我是否正确地假设这意味着字符串中只有第一次出现的Hello应该被hi替换,并且只有第二个被goodbye替换? -
@Błotosmętek 正确
标签: python string replace substring