英文:
Generating combinations of alphanumeric chars in a certain form
问题
我想生成一个列表
所需结果: AA0000 -> ZZ9999
我已经做了以下操作(Python3)
from itertools import *
inputa = product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)
inputb = product('123467890', repeat=4)
我认为这不是实现我的目标的最佳方法,因为它似乎需要进行一些更多的调整才能获得结果。(目前它只是将所需字符串两边的所有可能性用逗号分隔开来。)
Q. 我应该怎么做才能实现所需的结果?
英文:
I would like to generate a list
Required result: AA0000 -> ZZ9999
I have done the following (Python3)
from itertools import *
inputa = product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)
inputb = product('123467890', repeat=4)
I don't think this is the best way to achieve my goal, as it seems to require a few more adjustments to get the result. (It currently only makes all possibilities of each side of the desired string, separated by comma. )
Q. What could i do to achieve the required result?
答案1
得分: 3
从你已经开始的基础上继续构建。
from itertools import *
inputa = list(product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2))
inputb = list(product('123467890', repeat=4))
inputa = [''.join(x) for x in inputa]
inputb = [''.join(x) for x in inputb]
output = list(product(inputa, inputb))
output = [''.join(x) for x in output]
输出
['AA1111',
'AA1112',
'AA1113',
'AA1114',
'AA1116',
'AA1117',
'AA1118',
'AA1119',
'AA1110',
'AA1121',
'AA1122',
...
英文:
Building on what you've started.
from itertools import *
inputa = list(product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2))
inputb = list(product('123467890', repeat=4))
inputa = [''.join(x) for x in inputa]
inputb = [''.join(x) for x in inputb]
output = list(product(inputa,inputb))
output = [''.join(x) for x in output]
Output
['AA1111',
'AA1112',
'AA1113',
'AA1114',
'AA1116',
'AA1117',
'AA1118',
'AA1119',
'AA1110',
'AA1121',
'AA1122',
.....
答案2
得分: 3
以下是代码的翻译部分:
from itertools import product
a = [''.join(_) for _ in product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)]
n = [f'{_:04d}' for _ in range(10000)]
axn = [''.join(_) for _ in product(a, n)]
最终结果会类似于以下内容:
>>> len(axn)
6760000
>>> axn[0]
'AA0000'
>>> axn[-1]
'ZZ9999'
>>> 'AB1234' in axn
True
>>>
最后,如果您使用的是Python 3.6之前的版本,请使用以下版本:
from itertools import product
a = [''.join(_) for _ in product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)]
n = ['%04d' % _ for _ in range(10000)]
axn = [''.join(_) for _ in product(a, n)]
英文:
Here is another option:
from itertools import product
a = [''.join(_) for _ in product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)]
n = [f'{_:04d}' for _ in range(10000)]
axn = [''.join(_) for _ in product(a, n)]
That will result in something like this:
>>> len(axn)
6760000
>>> axn[0]
'AA0000'
>>> axn[-1]
'ZZ9999'
>>> 'AB1234' in axn
True
>>>
Finally, if you are using a Python version prior from 3.6, use this version:
from itertools import product
a = [''.join(_) for _ in product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=2)]
n = ['%04d' % _ for _ in range(10000)]
axn = [''.join(_) for _ in product(a, n)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论