I believe you can achieve what you want by subclassing the builtin dict class. See a debuggable/steppable demo of the code below at http://dbgr.cc/k
import random
class WordDict(dict):
def __getitem__(self, key):
vals = dict.__getitem__(self, key)
return random.choice(vals)
words = WordDict(
cold = ["cold", "frigid", "freezing"],
hot = ["scathing", "burning", "hot"]
)
for x in xrange(10):
print('the water is {word[cold]}'.format(word=words))
overriding the __getitem__ method will let you make assumptions about what each value of each key/value pair will be (a list), at which point you can just return a random item from the list of values.
The output of the above code is below:
the water is freezing
the water is cold
the water is freezing
the water is frigid
the water is cold
the water is frigid
the water is cold
the water is freezing
the water is freezing
the water is freezing
UPDATE
Just to make sure my answer completely matches your question/request, I've tweaked the code above to include the phrases array. Demoable/debuggable/steppable at http://dbgr.cc/n
import random
class WordDict(dict):
def __getitem__(self, key):
vals = dict.__getitem__(self, key)
return random.choice(vals)
words = WordDict(
cold = ["cold", "frigid", "freezing"],
hot = ["scathing", "burning", "hot"]
)
phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']
for x in xrange(10):
for phrase in phrases:
print phrase.format(word=words)
The output:
the water is frigid
the sun is scathing
the water is freezing
the sun is burning
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is frigid
the sun is scathing
the water is frigid
the sun is hot
the water is frigid
the sun is scathing
the water is freezing
the sun is hot
random % array-sizewhich won't be consistent (if that matters).