create words of various lengths

Fredrik Lundh fredrik at pythonware.com
Fri May 13 13:01:51 EDT 2005


superprad at gmail.com wrote:

> what I am looking for is
>
> 1. To create a list of different words of various lengths(1-15) using
> A-Z,a-z,0-9 and punctuations.Basically anything that could be found on
> a text document.
>
> 2. The words formed need not be meaningful .FOr example 'ajf' or
> 'fcjgdtfhbs' or even 'gfdew!' or '#bang.' would be a valid entry in the
> list.
>
> 3.So I am looking for a random set of words of sizes 1 to 15.The proble
> might be the time complexity. I inderstand that there would be too many
> permutations.

how many words do you need?  the following bruce-force solution
doesn't take that long to run on my machine, and the resulting words
are guaranteed to be almost entirely meaningless ;-)

    import string
    from random import choice, randint, shuffle

    alphabet = string.letters + string.digits + "%&!?#"

    words = {}

    while len(words) < 10000:
        words["".join(choice(alphabet) for i in range(randint(1,15)))] = None

    words = words.keys()
    shuffle(words)

to generate text from this, reshuffle the word list after you've written
a number of words.  (or you could slice off a random number of words
and run the loop again, at random intervals.  or something.)

the character and word distribution will have no similaries with real text,
of course, but maybe that doesn't matter.

</F>






More information about the Python-list mailing list