python字符串替换,所有可能的组合#2 [英] python string replacement, all possible combinations #2
问题描述
我的句子如下:
((wouldyou)) give me something ((please))
以及存储在数组/列表中的一堆关键字:
and a bunch of keywords, stored in arrays / lists:
keywords["wouldyou"] = ["can you", "would you", "please"]
keywords["please"] = ["please", "ASAP"]
我想用一组合适的字符串替换括号中的每一个变量在一个数组中,并获得所有可能的组合。变量和关键字的数量是未定义的。
I want to replace every occurrence of variables in parentheses with a suitable set of strings stored in an array and get every possible combination back. The amount of variables and keywords is undefined.
James 帮助我使用以下代码:
James helped me with the following code:
def filler(word, from_char, to_char):
options = [(c,) if c != from_char else (from_char, to_char) for c in word.split(" ")]
return (' '.join(o) for o in product(*options))
list(filler('((?please)) tell me something ((?please))', '((?please))', ''))
它工作得很好,但只用空字符串替换一个特定变量。现在我想通过不同的关键字集来浏览各种变量。期望的结果应该如下所示:
It works great but only replaces one specific variable with empty strings. Now I want to go through various variables with different set of keywords. The desired result should look something like this:
can you give me something please
would you give me something please
please give me something please
can you give me something ASAP
would you give me something ASAP
please give me something ASAP
我想它与 to_ch
有关,但我不知道如何通过列表项进行比较在这个地方。
I guess it has something to do with to_ch
, but I have no idea how to compare through list items at this place.
推荐答案
以下方法可行。它使用 itertools.product
来构建所有可能的关键字配对(或更多)。
The following would work. It uses itertools.product
to construct all of the possible pairings (or more) of your keywords.
import re, itertools
text = "((wouldyou)) give me something ((please))"
keywords = {}
keywords["wouldyou"] = ["can you", "would you", "please"]
keywords["please"] = ["please", "ASAP"]
# Get a list of bracketed terms
lsources = re.findall("\(\((.*?)\)\)", text)
# Build a list of the possible substitutions
ldests = []
for source in lsources:
ldests.append(keywords[source])
# Generate the various pairings
for lproduct in itertools.product(*ldests):
output = text
for src, dest in itertools.izip(lsources, lproduct):
# Replace each term (you could optimise this using a single re.sub)
output = output.replace("((%s))" % src, dest)
print output
你可以通过避免需要使用一个 re.sub()
调用执行多个 replace()
和赋值调用来进一步改进它。
You could further improve it by avoiding the need to do multiple replace()
and assignment calls with one re.sub()
call.
此脚本提供以下输出:
can you give me something please
can you give me something ASAP
would you give me something please
would you give me something ASAP
please give me something please
please give me something ASAP
它是使用Python 2.7测试的。如果使用多个相同的关键字,您将需要考虑如何解决它。希望你觉得这很有用。
It was tested using Python 2.7. You will need to think how to solve it if multiple identical keywords were used. Hopefully you find this useful.
这篇关于python字符串替换,所有可能的组合#2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!