Entry 3647
wordlist
Submitted by anonymous
on April 24, 2010 at 12:25 p.m.
Language: Python. Code size: 1.5 KB.
import random class Wordlist(list): """a list of unique, ordered, non-capitalized, unpunctuated words""" @staticmethod def lowerascii(s): return all(c in 'abcdefghijklmnopqrstuvwxyz' for c in s) def __init__(self, file='/usr/share/dict/words'): with open(file) as f: f.readline() # the first line of the file is blank, go figger for line in f: line = line.rstrip() if Wordlist.lowerascii(line): self.append(line) def shuffle(self): """shuffles the wordlist in place""" """Note: the total number of permutations of the list is larger than the period of the random number generators; this implies that most permutations can never be generated.""" random.shuffle(self) def partition(self, partitions=2): """returns a list containing partitions disjoint, strict subsets of the list""" # throw away random items to make len(self) a multiple of partitions while len(self) % partitions: kill = random.choice(self) self.remove(kill) psize = len(self)//partitions parts = [] for i in range(0, len(self), psize): parts.append(self[i:i+psize]) return parts def dict(self): """return a dictionary made of pairings of the wordlist""" x, y = self.partition() return dict(zip(x,y))
This snippet took 0.01 seconds to highlight.
Back to the Entry List or Home.