创建字符串的变体 [英] Create variations of a string

查看:55
本文介绍了创建字符串的变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了每个由16个字符组成的随机字符串,并使用以下命令将它们添加到列表中:

I generated random strings of 16-characters each and added them to a list using the following:

import random
strings = []
for x in range(0, 99): 
 strings.append(''.join(random.choice('0123456789ABCDEF') for i in range(16)))

这按预期工作.现在,对于每个生成的字符串,我想找到所有可能的组合,以使至少两个字符与原始字符串保持相同,并且字符顺序不变.例如,如果我们有CDD733665417E3F1,那么我想生成所有CDXXXXXXXXXXXXXX,其中X可以是任何值(0-9或A-F).同样是XXD7XXXXXXXXXXXX,依此类推.先前的类似问题暗示使用itertools.product,但是我不确定如何将其用于生成排列而不是固定的替换.任何帮助将不胜感激.谢谢

This worked as expected. Now, for each generated string, I want to find all possible combinations such that at least two characters remain same as the original string and the order of characters does not change. For example, if we have CDD733665417E3F1, then I want to generate all CDXXXXXXXXXXXXXX where X could be anything (0-9 or A-F). Similarly XXD7XXXXXXXXXXXX and so on. Previous similar questions hint towards using itertools.product but I am not sure how it can be used to generate permutations and not fixed replacements. Any help will be appreciated. Thank you

推荐答案

为要使用itertools.combinations

>>> from itertools import combinations
>>> s = 'ABC123'
>>> for indices in combinations(range(len(s)), 2):
...     print ''.join([s[x] if x in indices else 'X' for x in range(len(s))])
...
ABXXXX
AXCXXX
AXX1XX
AXXX2X
AXXXX3
XBCXXX
XBX1XX
XBXX2X
XBXXX3
XXC1XX
XXCX2X
XXCXX3
XXX12X
XXX1X3
XXXX23

创建所有变量字符串.

然后您可以进行嵌套循环来替换X的循环.

You can then do a nested loop to replace the X's.

然后,您可以使用product来获取将X替换为的所有字母:

Then you can use product to get all the letters that you would need to replace the X's with:

>>> for letters in product('ABCDEF0123456789', repeat = 4):
...     print letters
...
('A', 'A', 'A', 'A')
('A', 'A', 'A', 'B')
('A', 'A', 'A', 'C')
('A', 'A', 'A', 'D')
('A', 'A', 'A', 'E')
('A', 'A', 'A', 'F')
('A', 'A', 'A', '0')
('A', 'A', 'A', '1')
('A', 'A', 'A', '2')
('A', 'A', 'A', '3')
('A', 'A', 'A', '4')
('A', 'A', 'A', '5')
('A', 'A', 'A', '6')
('A', 'A', 'A', '7')
('A', 'A', 'A', '8')
('A', 'A', 'A', '9')
('A', 'A', 'B', 'A')
('A', 'A', 'B', 'B')
('A', 'A', 'B', 'C')
('A', 'A', 'B', 'D')
('A', 'A', 'B', 'E')
('A', 'A', 'B', 'F')
('A', 'A', 'B', '0')
('A', 'A', 'B', '1')
('A', 'A', 'B', '2')
.
.
.

将它们组合在一起,您将获得所需的所有组合.

Combine these together and you would get all the combinations of what you want.

您可能可以执行以下操作:

You can probably do something like:

>>> for indices in combinations(range(len(s)), 2):
...     for letters in product('ABCDEF0123456789', repeat = 4):
...          letter_iter = iter(letters)
...          print ''.join([s[x] if x in indices else letter_iter.next() for x in range(len(s))])

注1:您可以在combinations调用中更改2,以更改要保持不变的索引数量.同样,您可以在产品调用中更改repeat参数以反映这些更改(repeat = n其中n = len(s) - number_in_combinations)

NOTE 1: you can change the 2 in the call of combinations to change the amount of indices you want to stay the same. Likewise, you can change the repeat parameter in the call to product to reflect those changes (repeat = n where n = len(s) - number_in_combinations)

注2:这些值非常庞大.你知道这个.请注意不要破坏您的记忆.当我执行product调用时,我添加了一个索引计数器,并在索引计数器大于20时中断了循环,以避免地狱崩溃.

NOTE 2: These are stupidly large amounts of values. You know this. Please be careful that you don't destroy your memory. When I did the product call, I added an index counter and broke the loop after the index counter got greater than 20 to avoid hell breaking loose.

这篇关于创建字符串的变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆