Search code examples
pythonpermutationpython-itertools

Getting every possible permutation in a format python


I'm trying to program a way to generate all possible combinations in this format:

01-01-01-A

Now I've already looked in the itertools.permutations and combinations libs and read examples of how they worked. Though my problem is different than the other questions I've read

the first range can go from 0-38 and the next 2 ranges can go from 0-9 and the letter can go from A-C. I'm currently stuck on how I can use itertools to generate all possible combinations using this format.

What I am currently thinking is having 1 list with 4 lists inside with each of those numbers:

first_value = []
second_value = []
third_value = []
fourth_value = ["A", "B", "C"]
final_value = []
for num in range(0, 39):
    first_value.append(num)
for num in range(0, 10):
    second_value.append(num)
    third_value.append(num)
final_value.append(first_value)
final_value.append(second_value)
final_value.append(third_value)
final_value.append(fourth_value)
for value in itertools.permutations(final_value):
    print(value)

I'm not really sure how I can program this.


Solution

  • You want itertools.product.

    import itertools
    
    map(lambda t: "-".join(t), 
        itertools.product(
            map(str, range(0,39)), 
            map(str, range(0,10)), 
            map(str, range(0,10)), 
            ['A', 'B', 'C'])
        )