Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficent way to create all possible combinations of four lists in Python?

I have four different lists. headers, descriptions, short_descriptions and misc. I want to combine these into all the possible ways to print out:

header\n
description\n
short_description\n
misc

like if i had (i'm skipping short_description and misc in this example for obvious reasons)

headers = ['Hello there', 'Hi there!']
description = ['I like pie', 'Ho ho ho']
...

I want it to print out like:

Hello there
I like pie
...

Hello there
Ho ho ho
...

Hi there!
I like pie
...

Hi there!
Ho ho ho
...

What would you say is the best/cleanest/most efficent way to do this? Is for-nesting the only way to go?

like image 934
rinti Avatar asked Mar 05 '10 22:03

rinti


People also ask

How do you generate all possible combinations of multiple lists?

Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!

How do you get all possible combinations without repetition in Python?

In the formula of combinations without repetition, input the values: C ( n , r ) = n ! That's it! That's how you calculate the number of combinations without repetition.

How many combinations of 4 numbers are there?

There are 10,000 possible combinations that the digits 0-9 can be arranged into to form a four-digit code.


1 Answers

Is this what you're looking for? http://docs.python.org/library/itertools.html#itertools.product

like image 116
linked Avatar answered Sep 27 '22 23:09

linked