cycle through variables and assign all numers

Issue

So I want to cycle through variables A through J and assign numbers 0 through 9
but in a way that it goes through all possible combinations (yeah it’s a lot)
the code that I came up with is as follows

a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0

checkedValues = [False, False, False, False, False, False, False, False, False, False]
firstCycle = True

values = [a, b, c, d, e, f, g, h, i, j]
running  = True
i = 0
j = 0
while running == True:
    if not checkedValues[i]:
        for temp in range(10):
            j = temp + i
            if j < 10:
                values[temp] = j
            else:
                j -= 10
                values[temp] = j

    i += 1
    print(values)
    if i == 9:
        i = 0
        tempvar = values.pop(0)
        values.append(tempvar)

    if values == [a, b, c, d, e, f, g, h, i, j]:
        if firstCycle:
            firstCycle = False
        else:
            break

Question is if there are ways to do this faster and more efficiently.

edit: this code, kinda, works. Besides optimization problems, I also run into the issue that I want the variables to be seen separately from the list/array that they’re in. Only putting them in the list/array in the first place so that I can easily cycle through them to change the variables

Solution

It would be helpful if you provide some context to your question. But anyways I hope I got what you want to achieve – here would be my suggestion:

import itertools

min = 0
max = 9
possible_choices = [i for i in range(min, max + 1)]
all_combinations = list(itertools.permutations(possible_choices))
print(all_combinations)

for combination in all_combinations:
    # cycle through the results and do whatever you need to do
    print(combination)

Answered By – Tetrick25

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published