What I have: I have the list List123=[-13,3,12,1] and the 2-by-4-matrix Matrix123=numpy.zeros((2,4), dtype=Decimal).
What I want: I want to change all entries of the matrix to any entry of the list and then print it to the terminal. There are 4^(2*4)=65536 possible combinations. I want to print every combination.
How I do it now: Here is the my current code:
List123=[-13,3,12,1]
Matrix123=numpy.zeros((2,4), dtype=Decimal)
k=0
for k in List123:
Matrix123[0,0]=k
for k in List123:
Matrix123[0,1]=k
for k in List123:
Matrix123[0,2]=k
for k in List123:
Matrix123[0,3]=k
for k in List123:
Matrix123[1,0]=k
for k in List123:
Matrix123[1,1]=k
for k in List123:
Matrix123[1,2]=k
for k in List123:
Matrix123[1,3]=k
print Matrix123
print " "
My question: What is a more compact way to write this in only a few lines of code? I need to do the same for a 23-by-27 Matrix. That would mean that I have to write code for 23*27=621 for-loops manually, if I don't find a more compact way.
3 Answers 3
I highly recommend you to use itertools in python build-in tools:
from itertools import product
import numpy as np
List123 = [-13, 3, 12, 1]
for i in product(List123, repeat=8):
print(np.array(i).reshape(2, 4))
Here's a pure recursion function version:
import numpy as np
List123 = [-13, 3, 12, 1]
def dfs(depth=1, result=None):
if result is None:
result = []
if depth == 9:
print(np.array(result).reshape(2, 4))
return
for i in List123:
dfs(depth + 1, result + [i])
dfs()
In both way, you have 65536 results.
1 Comment
Using np.meshgrid:
def my_product(list, shape = (4,2)):
r = np.product(shape)
out = np.stack(np.meshgrid(*(List123,) * r))
out = out.reshape(shape[::-1] + (-1,)).T
for mat in list(out):
print(mat)
Of course, if your shape is (23,27), this will cause a memerror even if len(list) = 2 as all those permutations would fill all the storage on earth a ridiculous number of times over, and printing them all would take until the heat death of the universe.
Comments
You can use itertools.product:
import itertools
list123 = [-13, 3, 12, 1]
for matrix in itertools.product(list123, repeat=8):
print matrix
It will output all the possible solution of length 8 with -13, 3, 12 and 1. matrix will be a tuple with 8 numbers, being the possible solutions.
If you need it to output the result in the real numpy.matrix form, you can create them on the fly (even though it will take more time).
import numpy as np
for prod in itertools.product(list123, repeat=8):
print np.matrix([prod[:4], prod[4:]])
Or using reshape to improve readability (Thanks to @MenglongLi)
for prod in itertools.product(list123, repeat=8):
print np.reshape(prod, (2, 4))
It will gave you 65536 results as expected.
5 Comments
numpy.matrix anymore. Just use np.arraynumpy.matrix only exists for people coming from matlab to have something similar to the corresponding data-structure there. However, numpy and the scipy stack don't really have heavy support for it, and often, functions will return numpy.ndarrays regardless of if you input a np.matrixlinalg functions do. Probably for historical reasons.Explore related questions
See similar questions with these tags.
4^(27*23) ~= 6.x10^373and printing that many items will finish sometime around the heat death of the universe?