Derple
Derple

Reputation: 863

Generating combinations of alphanumeric chars in a certain form

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. ) Undesired output

Q. What could i do to achieve the required result?

Upvotes: 0

Views: 83

Answers (2)

accdias
accdias

Reputation: 5372

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)]

Upvotes: 3

Chris
Chris

Reputation: 16172

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',
.....

Upvotes: 3

Related Questions