Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 难以获得所有正则表达式可能性的组合列表_Python_Regex_Python 3.x_Python 3.6 - Fatal编程技术网

Python 难以获得所有正则表达式可能性的组合列表

Python 难以获得所有正则表达式可能性的组合列表,python,regex,python-3.x,python-3.6,Python,Regex,Python 3.x,Python 3.6,我正在使用exrex包为正则表达式生成所有排列的列表。但是,我有几个正则表达式,希望创建一组所有排列(没有重复)。因此,鉴于: from exrex import generate my_regexs=('a|b','a|c') expansions=map(generate,my_regexs) 也许我甚至不需要map或中间变量扩展来实现这一点-不确定。现在,如何从以下列表中获得排序列表: # Create a set from all of the expansions (e.g., l

我正在使用
exrex
包为正则表达式生成所有排列的列表。但是,我有几个正则表达式,希望创建一组所有排列(没有重复)。因此,鉴于:

from exrex import generate

my_regexs=('a|b','a|c')
expansions=map(generate,my_regexs)
也许我甚至不需要
map
或中间变量
扩展来实现这一点-不确定。现在,如何从以下列表中获得排序列表:

# Create a set from all of the expansions (e.g., let's store in myset, for clarity)
#     in order to merge duplicates
myset=... # Results in myset containing {'a','c','b'} - hash order
sorted_list=sorted(myset) # Finally, we get ['a','b','c']
谢谢你在这方面的帮助,我打赌有一个简单的带列表理解的一行程序可以做到这一点

注意:我们正在处理一个包含多个生成器的
映射
对象(即多个生成器的有序容器,而不是列表的
列表!)

更新:我想我已经把输入和输出弄清楚了:

Input: ('a|b','a|c') # Two reg-exs, results in all-permutations: ['a','b','a','c']
Output: ['a','b','c'] # Eliminating duplicates, we get the output presented

另一个答案涉及嵌套的理解案例,因此我正在更新此答案以使用
itertools.chain.from\u iterable

from exrex import generate
from itertools import chain
flatten = chain.from_iterable

regexes = ('a|b', 'a|c')

ordered_unique = sorted(set(flatten(map(generate, regexes))))

你能举一个输入和输出的例子吗?我现在还不清楚这个问题。在编辑重新生成器方面,
unique=set(perm for subproduct in subproduct for perm in subproduct的扩展中subproduct的perm)
的可能重复项:重要的是生成器和列表都是iterables,因此任何在iterables上工作的解决方案都将同时在列表和生成器上工作。@Jared,那么,我就错了,因为我无法成功地组合多个生成器(通过映射操作)来生成一个合并列表。uniq/sort非常简单。也就是说,我无法从映射结果中得到一组字符串。请尝试显示的代码,并查看是否可以使用
exrex.generate
-从输入中获得输出-这似乎很重要。可能会将
扩展
更改为
子集
,将
我的正则表达式
更改为
扩展
?@FMc,这样不会产生正确的结果。它产生:
['a'、'b'、'c'、'|']
,问题是您的答案的一个子集(稍微修改)是不正确的:
列表(e表示扩展中的e表示扩展中的e)
导致:
['a'、'|'、'b'、'a'、'|'、'c']
,这是不正确的。@Jared,我不确定,因为这些术语被多次使用。如果你想发布一个完全消除
扩展的正确答案,我投你一票。@MichaelGoldshteyn请看我对这个问题的评论itself@MichaelGoldshteyn修正了(我忘了使用
generate()
)。好吧,它可以工作,但我能理解吗?不合并
扩展
唯一
,我们得到:
有序=排序(set(map中的子管道的perm(generate,regexes)对于子管道中的perm))
这里是否可以不重用
perm
和/或
子管道
,或者它们必须出现两次?啊,出于某种原因,我从右到左而不是从左到右阅读了两个发电机。现在,这是有道理的,谢谢。我确实尝试了
chan。从_iterable
,当我自己尝试时,没有成功。@MichaelGoldshteyn现在,你可能想看看复制我链接的前两个答案,看看为什么我将其标记为复制。
from exrex import generate
from itertools import chain
flatten = chain.from_iterable

regexes = ('a|b', 'a|c')

ordered_unique = sorted(set(flatten(map(generate, regexes))))