How to replace each array element by 4 copies in Python?
<p>How do I use numpy / python array routines to do this ?</p>

<p>E.g. If I have array <code>[ [1,2,3,4,]]</code> , the output should be </p>

<pre><code>[[1,1,2,2,],
 [1,1,2,2,],
 [3,3,4,4,],
 [3,3,4,4]]
</code></pre>

<p>Thus, the output is array of double the row and column dimensions. And each element from original array is repeated three times. </p>

<p>What I have so far is this</p>

<pre><code>def operation(mat,step=2):
    result = np.array(mat,copy=True)
    result[::2,::2] = mat
    return result
</code></pre>

<p>This gives me array </p>

<pre><code>[[ 98.+0.j   0.+0.j  40.+0.j   0.+0.j]
 [  0.+0.j   0.+0.j   0.+0.j   0.+0.j]
 [ 29.+0.j   0.+0.j  54.+0.j   0.+0.j]
 [  0.+0.j   0.+0.j   0.+0.j   0.+0.j]]
</code></pre>

<p>for the input</p>

<pre><code>[[98 40]
 [29 54]]
</code></pre>

<p>The array will always be of even dimensions.</p>

<p>The better solution is to use numpy but you could use iteration also:</p>

<pre class="lang-python prettyprint-override"><code>a = [[1, 2, 3, 4]]

v = iter(a[0])

b = []
for i in v:
    n = next(v)
    [b.append([i for k in range(2)] + [n for k in range(2)]) for j in range(2)]

print b

&gt;&gt;&gt; [[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]
</code></pre>

python arrays numpy