Dictionary project

Michael Spencer mahs at telcopartners.com
Sat Mar 11 23:27:05 CET 2006

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
> Etcetera.
> I've looked on Google, and on the python lists at python.org, and haven't
> found any code that I can adapt.
> I've also tried to adapt some code from Perl, but not knowing the language
> very well, I wasn't very successful.
> If there is adaptable code on the internet that I've missed, please let me
> know, and I'll go on my marry way in search of it.
> Any help would be appreciated.
> Thanks Much,
> Brandon McGinty
> Brandon.mcginty at gmail.com
Something to adapt:

alphabet = "abcd"
def allwords(maxlength = 4):
     def wordgen(outer):
         for partial in outer:
             yield partial
             for letter in alphabet:
                 yield partial+letter
     gen = alphabet
     for length in range(maxlength-1):
         gen = wordgen(gen)
     return gen

 >>> list(allwords())
['a', 'aa', 'ab', 'ac', 'ad', 'aa', 'aaa', 'aab', 'aac', 'aad', 'ab', 'aba', 
'abb', 'abc', 'abd', 'ac', 'aca', 'acb', 'acc', 'acd', 'ad', 'ada', 'adb', 
'adc', 'add', 'aa', 'aaa', 'aab', 'aac', 'aad', 'aaa', 'aaaa', 'aaab', 'aaac', 
'aaad', 'aab', 'aaba', 'aabb', 'aabc', 'aabd', 'aac', 'aaca', 'aacb', 'aacc', 
'aacd', 'aad', 'aada', 'aadb', 'aadc', 'aadd', 'ab', 'aba', 'abb', 'abc', 'abd', 
'aba', 'abaa', 'abab', 'abac', 'abad', 'abb', 'abba', 'abbb', 'abbc', 'abbd', 
'abc', 'abca', 'abcb', 'abcc', 'abcd', 'abd', 'abda', 'abdb', 'abdc',
'dcdc', 'dcdd', 'dd', 'dda', 'ddb', 'ddc', 'ddd', 'dda', 'ddaa', 'ddab', 'ddac', 
'ddad', 'ddb', 'ddba', 'ddbb', 'ddbc', 'ddbd', 'ddc', 'ddca', 'ddcb', 'ddcc', 
'ddcd', 'ddd', 'ddda', 'dddb', 'dddc', 'dddd']

If you want the list sorted by length, as in your example:
 >>> sorted(_, key=len)
['a', 'b', 'c', 'd', 'aa', 'ab', 'ac', 'ad', 'aa', 'ab', 'ac', 'ad', 'aa', 'ab', 
'ac', 'ad', 'ba', 'bb', 'bc', 'bd', 'ba', 'bb', 'bc', 'bd', 'ba', 'bb', 'bc', 
'bd', 'ca', 'cb', 'cc', 'cd', 'ca', 'cb', 'cc', 'cd', 'ca', 'cb', 'cc', 'cd', 
'da', 'db', 'dc', 'dd', 'da', 'db', 'dc', 'dd', 'da', 'db', 'dc', 'dd', 'aaa', 
'aab', 'aac', 'aad', 'aba', 'abb', 'abc', 'abd', 'aca', 'acb', 'acc', 'acd', 
'ada', 'adb', 'adc', 'add', 'aaa', 'aab', 'aac', 'aad', 'aaa', 'aab', 'aac', 
'aad', 'aba', 'abb', 'abc', 'abd', 'aba', 'abb', 'abc', 'abd', 'aca',


More information about the Python-list mailing list