Faster way to do this?

Greg Fortune lists at gregfortune.com
Wed May 21 07:20:01 EDT 2003


Freddie wrote:

> Another, quite a bit faster method. Would generating a list of all
> possible words, then looking for them as dictionary keys be any faster? I
> can't actually work out how to generate such a list :\ From the input
> ('m', 'o', 'o') I want ('m', 'mo', 'mo', 'moo', 'o', 'om', 'oo', 'oom',
> 'o', 'om', 'oo', 'oom') in any sort of order. Well, maybe without
> duplicates :)
> 
> 

This will work, but be careful if your string length gets very long.  At 6 
letters, you've got 720 permutations which would go rather quickly and not 
each too much memory.  At 8 letters, it jumps to 40,320 permutations and at 
10, it's already above 3.5 million.  Doing a search on google for "string 
permutation python" turns up a few implementations.  If you are working 
with large length strings, the one found at 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465 looks like a 
good idea :)

Greg Fortune
Fortune Solutions




More information about the Python-list mailing list