I haven't found an answer for this. Sorry if it's common. I'm kinda newbie.
I'm creating for loops like this (to create a dictionary script):
for i1 in range(len(n)):
for i2 in range(len(n)):
for i3 in range(len(n)):
for i4 in range(len(n)):
for i5 in range(len(n)):
for i6 in range(len(n)):
word = n[i1] + n[i2] + n[i3] + n[i4] + n[i5] + n[i6]
And I would like to create a recursive version in which I could choose the number of loops. So if I have a bigger word, it'll loop enough. And I will need the pointer variables later (for that word creation), so I thought in using dynamic variables[Don't know if its possible, though]
n = len(string)
def loop(n): #'n' is a string and the length would be the number of recursions
if n > 0:
var1 [defining my dynam. var]
for var1 in range(len(string)):
loop(n-1)
else:
return word() #I guess I know how to code this one
So.. I want to have variables like var1, var2, var3 etc. to put in my for. Any help/directions is welcome! Thanks in advance!
Edit: Sorry for the trouble trying to understand it. Ok, I'm not sure if I should do this (should I erase the above?). I managed to create a iterative version of what I want: To input a string and print a list with all the possible combinations with those characters.
With the following function I got the output I wanted, but it is limited to 6 char. I guess with a recursive version it would be able to get any input and create as many loops as needed. [Better explained now?]
My actual script is as follows (I do know that there are better ways of doing the filter/checks):
def rec():
word = ""
txtfile = open(arq,'w') #arq is the string input + .txt
s=0 #Counts the number of words writen
t=0 #tests if the word exists
for i1 in range(len(n)):
for i2 in range(len(n)):
for i3 in range(len(n)):
for i4 in range(len(n)):
for i5 in range(len(n)):
for i6 in range(len(n)):
#This is a filter for not repeating the same character in a word
if not (i1 == i2 or i1 == i3 or i1 == i4 or i1 == i5 or i1 == i6 \
or i2 == i3 or i2 ==i4 or i2 == i5 or i2 ==i6 \
or i3 == i4 or i3 == i5 or i3 == i6 \
or i4 == i5 or i4 == i6 \
or i5 == i6 ):
word = n[i1] + n[i2] + n[i3] + n[i4] + n[i5] + n[i6]
txtfile.close()
data_file = open(arq)
#This one search for the word in the file, for not having duplicates
for line in data_file:
if line == word + "\n" :
t = 1
else:
pass
data_file.close()
if not t == 1:
s+=1
txtfile = open(arq,'a')
txtfile.writelines(word + "\n")
t=0
print ("Number of words writen:",s)
My output for "eeeeee" is just that one string, just as example. And the first ones for badges is: badges badgse badegs badesg badsge badseg bagdes bagdse bageds bagesd bagsde bagsed baedgs
Thanks a lot for the feedbacks!