更改字符串中的每个位置以找到所有可能的组合
Change each place in a string to find all possible combinations
我们的目标是找到所有可能的组合,如果我们用某些特定字符更改字符串中的一个位置。
这是我目前的代码:
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)
我们的目标是找到所有可能的组合,如果我们用某些特定字符更改字符串中的一个位置。
这是我目前的代码:
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)