Reputation: 477
I'm trying to write a small script (bash or python) that would give combinations of word lists that would output in formats as follows.
List 1 | List 2 | List 3
Like | Big | Trucks
Hate | Medium | Cars
| Small |
and would return results similar to the following
Like
Hate
Like Big
Like Medium
Like Small
Like Big Trucks
Like Big Cars
Like Medium Trucks
Like Medium Cars
Like Small Trucks
Like Small Cars
Hate Big
Hate Medium
Hate Small
Hate Big Trucks
Hate Big Cars
Hate Medium Trucks
Hate Medium Cars
Hate Small Trucks
Hate Small Cars
Big Trucks
Big Cars
Medium Trucks
Medium Cars
Small Trucks
Small Cars
Trucks
Cars
Notice how the words stay in the same order of the lists that they came from.
Upvotes: 1
Views: 99
Reputation: 6814
What you are looking for can be done very easily in python using the itertools module.
import itertools
list1 = ['Like', 'Hate']
list2 = ['Big', 'Medium', 'Small']
list3 = ['Trucks', 'Cars']
sub23 = [' '.join(x) for x in itertools.product(list2, list3)]
sub12 = [' '.join(x) for x in itertools.product(list1, list2 + sub23)]
res = list1 + sub12 + sub23 + list3
print res
Output:
['Like',
'Hate',
'Like Big',
'Like Medium',
'Like Small',
'Like Big Trucks',
'Like Big Cars',
'Like Medium Trucks',
'Like Medium Cars',
'Like Small Trucks',
'Like Small Cars',
'Hate Big',
'Hate Medium',
'Hate Small',
'Hate Big Trucks',
'Hate Big Cars',
'Hate Medium Trucks',
'Hate Medium Cars',
'Hate Small Trucks',
'Hate Small Cars',
'Big Trucks',
'Big Cars',
'Medium Trucks',
'Medium Cars',
'Small Trucks',
'Small Cars',
'Trucks',
'Cars']
Ps: the logic behind this distribution is "strange" to say the least. May be you could have better answers by providing the real logic behind it :)
Upvotes: 4