Converting a list of lists to an array in Python
<p>I've been looking this up, but I couldn't find anything that helped. I'm having some trouble making an array of the size I want. </p>

<p>So far, I have created a list of lists with this code: </p>

<pre><code>n=4
def matrixA(k):
    A=[]
    for m in range(0,k):
        row=[]
        #A.append([])
        for n in range(0,k):
            if (n==(m+1)) or (n==(m-1)):
                row.append(-deltaX/(deltaX)**2)
            if (n==m):
                row.append(2*deltaX/(deltaX)**2)
            else:
                row.append(0)
        A.append(row)
    return A
pprint.pprint(matrixA(n))
print len(matrixA(n))
</code></pre>

<p>I get this output. </p>

<pre><code>[[128.0, -64.0, 0, 0, 0],
 [-64.0, 0, 128.0, -64.0, 0, 0],
 [0, -64.0, 0, 128.0, -64.0, 0],
 [0, 0, -64.0, 0, 128.0]]
4
</code></pre>

<p>Now, I want to make this an array of size (4,4). My problem is that when I do the following (converting the list to an array and trying to shape it): </p>

<pre><code>A=numpy.array(matrixA(n))
print A
print "This is its shape:",A.shape
A.shape=(n,n)
</code></pre>

<p>I obtain: </p>

<pre><code>[[128.0, -64.0, 0, 0, 0] [-64.0, 0, 128.0, -64.0, 0, 0]
 [0, -64.0, 0, 128.0, -64.0, 0] [0, 0, -64.0, 0, 128.0]]
This is its shape: (4,)
</code></pre>

<p>And then an error: </p>

<pre><code>ValueError: total size of new array must be unchanged
</code></pre>

<p>How am I supposed to get an array of size (4,4) from here then?</p>