python字符串替换,所有可能的组合#2

编程入门 行业动态 更新时间:2024-10-08 14:38:33
本文介绍了python字符串替换,所有可能的组合#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

本文发布于:2023-11-30 07:30:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字符串   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!