-1

Having an alphabet of letters A-Z and numbers 0-9, how to get all 1296 possible combinations like:

['AA', 'AB', ..., 'AZ', 'A0', 'A1', ..., 'Z9', '0A', '0B', ..., '98', '99']

As a side question, what is this type of number system called?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Kishan M
  • 173
  • 2
  • 13
  • Use `itertools` [combinations with replacement](https://docs.python.org/3.5/library/itertools.html#itertools.combinations_with_replacement) – Mr. T Dec 28 '17 at 10:13

2 Answers2

3

Current suggestions are wrong. Combinations with replacement, for example, won't give you AB and BA at the same time, only the first one. And permutations won't have AA, BB, etc.

Instead you should use itertools.product.

For example:

import string
import itertools

combinations_generator = itertools.product(string.ascii_uppercase + string.digits, 
                                           repeat=2)
combinations = list(map(''.join, combinations_generator))
print(len(combinations))

This will give you exactly 1296 combinations.

combinations_generator will generate tuples like ('A', 'A'), ('A', 'B'), etc.
And with map(''.join, combinations_generator) we will join them together like 'AA', 'AB', etc.

Georgy
  • 12,464
  • 7
  • 65
  • 73
0

you can use permutations to get the list.

For example

import string
import itertools
series = [''.join(r) for r in itertools.permutations([str(i) for i in range(10)]+[str(c) for c in string.ascii_uppercase], 2)]
print(series)
print(len(series))  # got 1260 here
CSJ
  • 2,709
  • 4
  • 24
  • 30
  • shouldn't it be 1296 combinations? – Kishan M Dec 28 '17 at 09:10
  • Oh, yes, then the series need to add AA BB CC DD.. 00 ...99, so you can just use 2 layer for loop to get the series , no need any built in functions – CSJ Dec 28 '17 at 14:48