Python 在字符串中查找所有可能的替换组合
以下是我目前掌握的代码:Python 在字符串中查找所有可能的替换组合,python,combinations,itertools,Python,Combinations,Itertools,以下是我目前掌握的代码: from itertools import combinations, product string = "abcd012345" char = "01268abc" for i, j in combinations(tuple(range(len(string))), 2): for char1, char2 in product(char, char): print(string[:i] + ch
from itertools import combinations, product
string = "abcd012345"
char = "01268abc"
for i, j in combinations(tuple(range(len(string))), 2):
for char1, char2 in product(char, char):
print(string[:i] + char1 + string[i+1:j] + char2 + string[j+1:])
所以,字符串是abcd012345,为了找到所有可能的组合,我更改了两个字符。字符是01268abc。在这个例子中,我们得到了2880个组合
目标是设置字符串中指定位置的字符。示例如下:
from itertools import combinations, product
string = "abcd012345"
# place 0123456789
char_to change_for_place0 = "ab02"
char_to change_for_place1 = "14ah"
char_to change_for_place2 = "94nf"
char_to change_for_place3 = "a"
char_to change_for_place4 = "9347592"
char_to change_for_place5 = "93478nvg"
char_to change_for_place6 = "b"
char_to change_for_place7 = ""
char_to change_for_place8 = ""
char_to change_for_place9 = "84n"
for i, j in combinations(tuple(range(len(string))), 2):
for char1, char2 in product(char, char):
print(string[:i] + char1 + string[i+1:j] + char2 + string[j+1:])
注意:
我将乐于从您的解决方案和想法中学习,谢谢。这归结为将
字符串中每个职位的当前字母添加到您当前该职位的替代者,然后创建这些选项的所有可能组合:
from itertools import combinations, product
string = "abcd012345"
# must be of same lenght as (string), each entry correspond to the same index in string
p = ["ab02", "14ah", "94nf", "a", "9347592", "93478nvg", "b", "", "", "84n"]
errmsg = f"Keep them equal lenghts: '{string}' ({len(string)}) vs {p} ({len(p)})"
assert len(p)==len(string), errmsg
# eliminates duplicates from letter in string + replacments due to frozenset()
d = {idx: frozenset(v + string[idx]) for idx, v in enumerate(p)}
# creating this list take memory
all_of_em = [''.join(whatever) for whatever in product(*d.values())]
# if you hit a MemoryError creating the list, write to a file instead
# this uses a generator with limits memory usage but the file is going
# to get BIG
# with open("words.txt","w") as f:
# for w in (''.join(whatever) for whatever in product(*d.values())):
# f.write(w+"\n")
print(*all_of_em, f"\n{len(all_of_em)}", sep="\t")
输出:
2and2g234n 2and2g2348 2and2g2344 2and2g2345 2and27b34n
[...snipp...]
249d99234n 249d992348 249d992344 249d992345
100800
如果您重视替换中字母的顺序,请使用
d = {idx: (v if string[idx] in v else string[idx]+v) for idx, v in enumerate(p)}
相反:
abcd012345 abcd012348 [...] 2hfa2gb344 2hfa2gb34n 115200
金额差异是由于“9347592”中重复的9
,使用冻结集删除
要仅获取更改少于5项的内容,请执行以下操作:
# use a generator comprehension to reduce memory usage
all_of_em = (''.join(whatever) for whatever in product(*d.values()))
# create the list with less then 5 changes from the generator above
fewer = [w for w in all_of_em if sum(a != b for a, b in zip(w, string)) < 5]
#使用生成器理解来减少内存使用
all_of_em=(“”.join(whatever)表示产品中的whatever(*d.values())
#从上面的生成器创建少于5个更改的列表
更少=[w表示所有元素中的w,如果sum(a!=b表示a,b表示zip(w,string))<5]
@perm用open(“words.txt”,“w”)作为f:for w,在更少的:f.write(w+“\n”)
@permpyt使用==
@permpyt您似乎试图创建一个密码破解器操作系统-我不想参与其中。如果您有64个位置,并且最多有4个位置针对4-6种不同的内容进行了更改,那么创建所有位置,然后过滤到0,1,2,3,4个位置进行更改是低效的。我多次更改和修改我的答案,并回答了非常基本的问题(=vs==)f.e.-你可能想做一些不太复杂的事情,或者问一个新问题,以便其他ppl帮助你。