Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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_Python 3.x_Combinations_Product_Itertools - Fatal编程技术网

Python 更改字符串中的每个位置以查找所有可能的组合

Python 更改字符串中的每个位置以查找所有可能的组合,python,python-3.x,combinations,product,itertools,Python,Python 3.x,Combinations,Product,Itertools,我们的目标是找到所有可能的组合,如果我们用一些特定的字符改变字符串中的一个位置 以下是我目前掌握的代码: from itertools import product string = "abc123" char = "def2346789" for i in range(len(string)): for char1 in product(char): print(string[:i] + char1 + string[i+1

我们的目标是找到所有可能的组合,如果我们用一些特定的字符改变字符串中的一个位置

以下是我目前掌握的代码:

from itertools import product

string = "abc123"

char = "def2346789"

for i in range(len(string)):
    for char1 in product(char):
        print(string[:i] + char1 + string[i+1:])
char
表示将在每个位置使用的字符

但我不知道它将如何工作在第二部分,将享受探索解决方案

所以结果会是这样的:

dbc123 adc123
ebc123 aec123
fbc123 afc123 
2bc123 a2c123 
3bc123 a3c123
4bc123 a4c123
6bc123 a5c123
7bc123 a6c123
8bc123 a7c123
9bc123 a8c123 etc...

对于第二部分,我假设您指的是for循环

基本上是:

for every character position in string (called i):
    for every character in char (called char1):
        substitute the character at the current position with char1
        #This is done by printing all characters before the current position 
        #(string[:i]), then printing the new character (char1) then printing all 
        #charactrers after the current position (string[i+1:])
您不(不应该)需要在此处使用
itertools.product
。这可能就是让你困惑的地方:

string = "abc123"

char = "def2346789"

for i in range(len(string)):
    for c in char:
        print(string[:i] + c + string[i+1:])

使用函数和理解也可以使代码更具可读性

     string = "abc123"
     char = "def2346789"

     def fncCombinations(a):
           combination_list=[]
           for j in range(len(string)): 
                combination_list.append(string[:j] + a + string[j+1:])
     return combination_list 
     combination_list=[fncCombinations(a) for a in char]
     print(combination_list)

`您可以使用堆栈和队列

string="abc123"
char = "def2346789"
queue=[]
stack=[]
combination_list=[]
queue=[x for x in string]
    
for index in range(len(queue)):
    stack.append(queue.pop(0))
    front="".join(stack[:-1])
    back="".join(queue)
    combination_list.append([front+a+back for a in char])

 print(combination_list)

您能否澄清哪一部分是“第二部分”@Moosefeather对于loopsCan,您提供了一些输出示例?