Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

All permutations of random value within list of values using python

I need to reach the following result using python (thought about itertools), I have 4 rows that each can get one the values = [‘v1’, ‘v2’, ‘v3’], since we have 3 options and 4 rows, then number of possibilities will be 3^4 = 81

For example:

    col1  col2  col3 ....... col81  
1    v1    v1    v_  
2    v2    v3
3    v1    v2
4    v3    v1

how can I achieve it, in such a way that I cover all the possibilities?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Seems like you’re looking for the "product" not the "permutation". Try this:

import itertools
from pprint import pprint

v = [1, 2, 3]

pprint(list(itertools.product(v, repeat=4)))
[(1, 1, 1, 1),
 (1, 1, 1, 2),
 (1, 1, 1, 3),
 (1, 1, 2, 1),
 (1, 1, 2, 2),
...
 (3, 3, 2, 3),
 (3, 3, 3, 1),
 (3, 3, 3, 2),
 (3, 3, 3, 3)]

If you need the transpose of that, you can do this:

import itertools
from pprint import pprint

v = [1, 2, 3]

product = list(itertools.product(v, repeat=4))
transpose = list(map(list, zip(*product)))
pprint(transpose)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading