Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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_Replace - Fatal编程技术网

Python 可能替换的所有版本

Python 可能替换的所有版本,python,regex,python-3.x,replace,Python,Regex,Python 3.x,Replace,我有一个小问题: 例如,我们有字符串'YXY00'。每个'Y'的'X'可以分别替换为'Y'或'X'。在本例中,我们有2^3=8个替换选项,如: YXY00 YXX00 YYY00 YYX00 XXY00 XXX00 XYY00 XYX00 我如何用Python 3.x替换它 我进行了更多重构,现在它更具可读性: def replace_at_index(string, index, replacement): """ Credit to: http://stackoverflo

我有一个小问题:

例如,我们有字符串
'YXY00'
。每个
'Y'
'X'
可以分别替换为
'Y'
'X'
。在本例中,我们有2^3=8个替换选项,如:

YXY00
YXX00
YYY00
YYX00
XXY00
XXX00
XYY00
XYX00

我如何用Python 3.x替换它

我进行了更多重构,现在它更具可读性:

def replace_at_index(string, index, replacement):
    """
    Credit to: http://stackoverflow.com/users/95612/jochen-ritzel

    >>> replace_at_index("abc", 1, "z")
    'azc'
    """
    return string[:index] + replacement + string[index + 1:]

def possible_replaces(string):
    """
    >>> list(possible_replaces('YXY00'))
    ['XXY00', 'YXY00', 'YXY00', 'YYY00', 'YXX00', 'YXY00', 'YXY00', 'YXY00']
    >>> list(possible_replaces('XYY000000'))
    ['XYY000000', 'YYY000000', 'XXY000000', 'XYY000000', 'XYX000000', 'XYY000000', 'XYY000000', 'XYY000000', 'XYY000000', 'XYY000000', 'XYY000000', 'XYY000000']
    """
    for index, char in enumerate(string):
        if char in 'XY':
            yield replace_at_index(string, index, 'X')
            yield replace_at_index(string, index, 'Y')
        else:
            yield string

我还写了一个更通用的解决方案:

def possible_replaces(string, to_multipy_replace = 'XY'):
    """
    Returns a list of strings where each member of `to_multipy_replace` is replaced
    by each member of said set.

    >>> list(possible_replaces('YXY00'))
    ['XXY00', 'YXY00', 'YXY00', 'YYY00', 'YXX00', 'YXY00', 'YXY00', 'YXY00']
    >>> list(possible_replaces('XYY0'))
    ['XYY0', 'YYY0', 'XXY0', 'XYY0', 'XYX0', 'XYY0', 'XYY0']
    """
    for index, char in enumerate(string):
        if char in to_multipy_replace:
            for replacement in to_multipy_replace:
                yield replace_at_index(string, index, replacement)
        else:
            yield string

您现在不仅限于'XY',还可以使用任何您喜欢的字符集。

您可以使用
itertools.permutations()
获取字符串的排列。然后,通过应用一些替换逻辑,您可以得到以下结果:

import itertools

def permutate(source, changeset):
    count = sum(1 for char in source if char in changeset)
    holder = ''.join('{}' if char in changeset else char for char in source)
    for perm in set(itertools.permutations(changeset * count, count)):
        print(holder.format(*perm))

permutate('XY000Y00', 'XY')
结果:

XY000X00
XX000Y00
XY000Y00
XX000X00
YX000X00
YY000Y00
YX000Y00
YY000X00

谢谢你的回答:)但它不起作用,例如在打印上(列表(可能替换(“XYY000000”))它打印['XYY000000','YYYY000000','XXY000000','XYY000000','XYX000000','XYY000000','XYY000000','XYY000000','XYY000000','XYY000000','XYY000000','XYY000000']谢谢你的回答,它不知道这种方式:)但是,对不起,样品不完整。例如,我们可能有'XX000YY0X',我已经修改了解决方案,希望能解决更一般的情况