Home > other >  How can I create a table with 64 combinations with python?
How can I create a table with 64 combinations with python?

Time:10-21

I want to create a table that one column is choosing 3 out of a list [1,2,3,4] randomly with replacement. Then another column is only changing the middle number.

For example,

column 1   column2
111         121
111         131
111         141
121         111
121         131
...
444         414
444         424
444         434

The total should be 64 combinations. I tried combinations_with_replacement()

lst = [1,2,3,4]
ref = list(it.combinations_with_replacement(lst, 3))
>>> print(ref)
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 3), (1, 3, 4), (1, 4, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 3), (2, 3, 4), (2, 4, 4), (3, 3, 3), (3, 3, 4), (3, 4, 4), (4, 4, 4)]
>>> len(ref)
20

but it did not work as I wanted... Any suggestions on how to change?

Thanks

CodePudding user response:

Just as a hint:

col1 = []
col2 = []
for i in range(4):
    for j in range(4):
        for k in range(4):
            col1.append([i   1, j   1, k   1])
            for l in range(3):
                col2.append([i   1, (j   l   1) % 4   1, k   1])
print ("The list\n"   str(col1)   "\nhas "   str(len(col1))   " entries")
print ("The list\n"   str(col2)   "\nhas "   str(len(col2))   " entries")

This yields:

The list
[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 1], [1, 3, 2], [1, 3, 3], [1, 3, 4], [1, 4, 1], [1, 4, 2], [1, 4, 3], [1, 4, 4], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 1, 4], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 1], [2, 3, 2], [2, 3, 3], [2, 3, 4], [2, 4, 1], [2, 4, 2], [2, 4, 3], [2, 4, 4], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 1, 4], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 2, 4], [3, 3, 1], [3, 3, 2], [3, 3, 3], [3, 3, 4], [3, 4, 1], [3, 4, 2], [3, 4, 3], [3, 4, 4], [4, 1, 1], [4, 1, 2], [4, 1, 3], [4, 1, 4], [4, 2, 1], [4, 2, 2], [4, 2, 3], [4, 2, 4], [4, 3, 1], [4, 3, 2], [4, 3, 3], [4, 3, 4], [4, 4, 1], [4, 4, 2], [4, 4, 3], [4, 4, 4]]
has 64 entries
The list
[[1, 2, 1], [1, 3, 1], [1, 4, 1], [1, 2, 2], [1, 3, 2], [1, 4, 2], [1, 2, 3], [1, 3, 3], [1, 4, 3], [1, 2, 4], [1, 3, 4], [1, 4, 4], [1, 3, 1], [1, 4, 1], [1, 1, 1], [1, 3, 2], [1, 4, 2], [1, 1, 2], [1, 3, 3], [1, 4, 3], [1, 1, 3], [1, 3, 4], [1, 4, 4], [1, 1, 4], [1, 4, 1], [1, 1, 1], [1, 2, 1], [1, 4, 2], [1, 1, 2], [1, 2, 2], [1, 4, 3], [1, 1, 3], [1, 2, 3], [1, 4, 4], [1, 1, 4], [1, 2, 4], [1, 1, 1], [1, 2, 1], [1, 3, 1], [1, 1, 2], [1, 2, 2], [1, 3, 2], [1, 1, 3], [1, 2, 3], [1, 3, 3], [1, 1, 4], [1, 2, 4], [1, 3, 4], [2, 2, 1], [2, 3, 1], [2, 4, 1], [2, 2, 2], [2, 3, 2], [2, 4, 2], [2, 2, 3], [2, 3, 3], [2, 4, 3], [2, 2, 4], [2, 3, 4], [2, 4, 4], [2, 3, 1], [2, 4, 1], [2, 1, 1], [2, 3, 2], [2, 4, 2], [2, 1, 2], [2, 3, 3], [2, 4, 3], [2, 1, 3], [2, 3, 4], [2, 4, 4], [2, 1, 4], [2, 4, 1], [2, 1, 1], [2, 2, 1], [2, 4, 2], [2, 1, 2], [2, 2, 2], [2, 4, 3], [2, 1, 3], [2, 2, 3], [2, 4, 4], [2, 1, 4], [2, 2, 4], [2, 1, 1], [2, 2, 1], [2, 3, 1], [2, 1, 2], [2, 2, 2], [2, 3, 2], [2, 1, 3], [2, 2, 3], [2, 3, 3], [2, 1, 4], [2, 2, 4], [2, 3, 4], [3, 2, 1], [3, 3, 1], [3, 4, 1], [3, 2, 2], [3, 3, 2], [3, 4, 2], [3, 2, 3], [3, 3, 3], [3, 4, 3], [3, 2, 4], [3, 3, 4], [3, 4, 4], [3, 3, 1], [3, 4, 1], [3, 1, 1], [3, 3, 2], [3, 4, 2], [3, 1, 2], [3, 3, 3], [3, 4, 3], [3, 1, 3], [3, 3, 4], [3, 4, 4], [3, 1, 4], [3, 4, 1], [3, 1, 1], [3, 2, 1], [3, 4, 2], [3, 1, 2], [3, 2, 2], [3, 4, 3], [3, 1, 3], [3, 2, 3], [3, 4, 4], [3, 1, 4], [3, 2, 4], [3, 1, 1], [3, 2, 1], [3, 3, 1], [3, 1, 2], [3, 2, 2], [3, 3, 2], [3, 1, 3], [3, 2, 3], [3, 3, 3], [3, 1, 4], [3, 2, 4], [3, 3, 4], [4, 2, 1], [4, 3, 1], [4, 4, 1], [4, 2, 2], [4, 3, 2], [4, 4, 2], [4, 2, 3], [4, 3, 3], [4, 4, 3], [4, 2, 4], [4, 3, 4], [4, 4, 4], [4, 3, 1], [4, 4, 1], [4, 1, 1], [4, 3, 2], [4, 4, 2], [4, 1, 2], [4, 3, 3], [4, 4, 3], [4, 1, 3], [4, 3, 4], [4, 4, 4], [4, 1, 4], [4, 4, 1], [4, 1, 1], [4, 2, 1], [4, 4, 2], [4, 1, 2], [4, 2, 2], [4, 4, 3], [4, 1, 3], [4, 2, 3], [4, 4, 4], [4, 1, 4], [4, 2, 4], [4, 1, 1], [4, 2, 1], [4, 3, 1], [4, 1, 2], [4, 2, 2], [4, 3, 2], [4, 1, 3], [4, 2, 3], [4, 3, 3], [4, 1, 4], [4, 2, 4], [4, 3, 4]]
has 192 entries

CodePudding user response:

itertools.combinations_with_replacement will sort the results and remove duplicates. (1,2,1) will be sorted to (1,1,2) and removed because (1,1,2) already exists. What you are looking for is itertools.product.

lst = [1,2,3,4]
ref = list(it.product(lst, repeat=3))
>>> len(ref)
64
  • Related