Dictionary project

Ben C spamspam at spam.eggs
Sat Mar 11 18:26:54 EST 2006


On 2006-03-11, Michael Spencer <mahs at telcopartners.com> wrote:
> brandon.mcginty at gmail.com wrote:
>> Hi All,
>> First, I hope this post isn't against list rules; if so, I'll take note in
>> the future.
>> 
>> I'm working on a project for school (it's not homework; just for fun).
>> For it, I need to make a list of words, starting with 1 character in length,
>> up to 15 or so.
>> It would look like:
>> 
>> A B C d E F G ... Z Aa Ab Ac Ad Ae Aaa Aab Aac

This is really "permutations" you're trying to work out I think. Python
generators are quite good for this kind of thing.

This works, but isn't especially pretty:

import string

def letters():
	while 1:
		for char in string.lowercase:
			yield char
		yield None

def count(n):
	digits = [letters() for i in range(n)]
	s = [d.next() for d in digits]

	def output():
		print string.join(reversed(s), "")

	output()

	while 1:
		i = 0
		while 1:
			s[i] = digits[i].next()
			if s[i] is not None: break
			s[i] = digits[i].next()

			i += 1
			if i == n: return
		output()

count(3)

Obviously you can call count with any number, and if you just change
letters() to yield members of some other set you can make permutations
of different things.

I think 15 is going to give you rather a lot of permutations though.
1677259342285725925376 to be precise. You might have to wait a rather
long time.



More information about the Python-list mailing list