I have a script that loops through a series of four (or less) characters strings. For example:
aaaa aaab aaac aaad
If have been able to implement it with nested for loops like so:
chars = string.digits + string.uppercase + string.lowercase for a in chars: print '%s' % a for b in chars: print '%s%s' % (a, b) for c in chars: print '%s%s%s' % (a, b, c) for d in chars: print '%s%s%s%s' % (a, b, c, d)
Is this sort of loop nesting a bad thing, and if so, what would be a better way of accomplishing what I am doing?
That’ll print all
15018570words you’re looking for. If you want more/less words just change theMAX_CHARSvariable. It will still have just twofors for any number of chars, and you don’t have to repeat yourself. And is pretty readable. .