userBG
userBG

Reputation: 7170

Python string formating using values from multiple lists

I'm trying to format a string using values from several lists. The following is pseudo-code but should give an idea of the expected output. The output would be combination of each item in each list: each person likes to eat all fruits while doing all hobbies. So how to do this in python?

There should be len(names)*len(fruits)*len(hobbies) possibilities (64 in my example)

names = ['tom','marry','jessica','john']
fruits = ['oranges','apples','grapes','bananas']
hobbies = ['dancing','sitting','bicycling','watching tv']

print '%(name)s likes to eat %(fruit)s while %(hobby)s \n'
       % {'name':names, 'fruit':fruits, 'hobby':hobbies}

Upvotes: 1

Views: 218

Answers (3)

Roberto Bonvallet
Roberto Bonvallet

Reputation: 33319

The itertools module provides the product function, that yields all posible tuples:

>>> from itertools import product
>>> names = ['tom','marry','jessica','john']
>>> fruits = ['oranges','apples','grapes','bananas']
>>> hobbies = ['dancing','sitting','bicycling','watching tv']
>>> for n, f, h in product(names, fruits, hobbies):
...     print '%s likes to eat %s while %s' % (n, f, h)

You could also use the tuple directly:

>>> for t in product(names, fruits, hobbies):
...     print '%s likes to eat %s while %s' % t

Upvotes: 2

juliomalegria
juliomalegria

Reputation: 24911

Just use a for-loop:

names = ['tom','marry','jessica','john']
fruits = ['oranges','apples','grapes','bananas']
hobbies = ['dancing','sitting','bicycling','watching tv']

for name in names:
    for fruit in fruits:
        for hobby in hobbies:
            print '%(name)s likes to eat %(fruit)s while %(hobby)s' \
                   % {'name': name, 'fruit': fruit, 'hobby': hobby}

But, if you ask me, I always think everything looks better with .format():

for name in names:
    for fruit in fruits:
        for hobby in hobbies:
            print '{} likes to eat {} while {}'.format(name, fruit, hobby)

Upvotes: 2

DSM
DSM

Reputation: 352979

If I understand your "The output would be combination of each item in each list: each person likes all fruits while doing each hobby" line, you want every possible combination. You can do this in a nested loop way:

names = ['tom','mary','jessica','john']
fruits = ['oranges','apples','grapes','bananas']
hobbies = ['dancing','sitting','bicycling','watching tv']

for name in names:
    for fruit in fruits:
        for hobby in hobbies:
            print '%(name)s likes to eat %(fruit)s while %(hobby)s' % {'name':name, 'fruit':fruit, 'hobby':hobby}

which produces

tom likes to eat oranges while dancing
tom likes to eat oranges while sitting
tom likes to eat oranges while bicycling
tom likes to eat oranges while watching tv
tom likes to eat apples while dancing
[etc.]
john likes to eat bananas while bicycling
john likes to eat bananas while watching tv

or you could use the itertools module, which has a function product which gives you every possible combination of the input lists:

import itertools

for name, fruit, hobby in itertools.product(names, fruits, hobbies):
    print '%(name)s likes to eat %(fruit)s while %(hobby)s' % {'name':name, 'fruit':fruit, 'hobby':hobby}

Upvotes: 5

Related Questions