#!/usr/bin/python import random lower_a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] upper_a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] all = [] all = " ".join("".join(lower_a) + "".join(upper_a) + "".join(num)) all = all.split() x = 1 c = 1 while x < 10: y = [] for i in range(c): a = random.choice(all) y.append(a) print "".join(y) x += 1 c += 1
what i have now outputs something like the following:
5 hE HAy 1kgy Pt6JM 2pFuCb Jv5osaX 5q8PwWAO SvHWRKfI5
how can i make it systematically go through every combination of letters (upper and lowercase) for a given length, then add 1 to that length and repeat the process?
Take a look at function combinations in the module itertools (http://docs.python.org/library/itertools.html#itertools.combinations)
The pythonic way ;)
Print all combinations:
Even better, create an generator object which yields the combinations, so that one can decide later what to do with them without having to store
2 ** 62
strings (7.6040173890593902e+35
bytes) in memory.Both
combinations
andproduct
, as well as many other functions, return iterators instead of lists in order to save memory:It's best not to recreate functionality that is already in the standard library.
Take a look at the standard library module "itertools".
Particularly the combinations(), permutations(), and product() functions.
If your version of Python is old you may not have access to these functions. However if you take a look in the documentation for Python 2.6, you can see how all of these functions can be implemented in Python. For instance, the implementation of itertools.product is given as:
You could also try a recursive solution instead:
I haven't tested this, but I think the basic idea should hold. Please comment if it doesn't work and I'll debug it: