Q:

How to map word combinations in python

Lets say I have a 2 word combination:
two flowers

I want to map possible combinations of these words programatically.  I have the following pseudocode combinations:
{fw}{lw}  (firstword, last word)
{fw} {lw}  (firstword, space, last word)
......

What is the best way in python to build up a shorthand notation like above, what data structure should it be stored in, and finally how would one map:
 two flowers x [{fw}{lw},{fw} {lw}] -> ['twoflowers','two flowers'] etc

I'm sorry this is kind of an open ended question. I'm pretty new to python and not sure how to approach this problem, so any advice would be appreciated. I put square brackets around the structures even though lists may not be the best data structure to use here.

A:

What you've described is already built into Python (unless you're somehow on a version before 2.6):
>>> '{fw} {lw}'.format(fw='hello', lw='world')
'hello world'

or equivalently
>>> inputs = {'fw': 'hello', 'lw': 'world'}
>>> '{fw} {lw}'.format(**inputs)
'hello world'

(The ** here takes a dict and uses it to set a function's keyword arguments.)  See the standard library documentation for more.
To iterate over a number of formats, you can use a standard for loop, or to be slick a list comprehension:
>>> format_strings = ['{fw}{lw}', '{fw} {lw}']
>>> [format_string.format(**inputs) for format_string in format_strings]
['helloworld', 'hello world']

Update: upon rereading your question it sounds like you might prefer the positional version of the above, which looks like this:
>>> '{0} {1}'.format('hello', 'world')
'hello world'
>>> inputs = ['hello', 'world']  # or 'hello world'.split()
>>> '{0} {1}'.format(*inputs)
'hello world'

The * is a lot like the **: instead of using a dict to set keyword arguments, it is using a list (or tuple) to set positional arguments.

