Transform 1D List
<p>I'm new to python and I need to transform a list of number(0,1,2,3) into a 2D array. Basically, they both have the same row number however the value in the 1D list indicates it's column number in the 2D array. The value in the 2D list is marked with a 1. For example, a sample 1D list </p>

<pre><code>list_1d = [1,0,3,2,0]

1
0
3
2
0
</code></pre>

<p>I need it to be transformed to, </p>

<pre><code>0 1 0 0
1 0 0 0
0 0 1 0
0 1 0 0
1 0 0 0
</code></pre>

<p>I have created a 2D array of zeros, </p>

<pre><code>array_2d = np.zeros((len(list_1d),4))
</code></pre>

<p>Kindly appreciated. </p>

<pre><code>list_1d = [1,0,3,2,0]
</code></pre>

<p>How tall should the matrix be?</p>

<blockquote>
  <p><code>height</code> is the number of elements in the 1d list.</p>
</blockquote>

<pre><code>height = len(list_1d)
</code></pre>

<p>How wide should the matrix be?</p>

<blockquote>
  <p><code>width</code> is the number of elements in each inner list.</p>
</blockquote>

<pre><code>width = max(list_1d)
</code></pre>

<p>What should the value of each inner list element be?</p>

<blockquote>
  <p>0 most of the time.</p>
</blockquote>

<p>When should a matrix element not be zero?</p>

<blockquote>
  <p>When its row is its original index and its column is its original value.</p>
</blockquote>

<p>Can you express that as a Boolean?</p>

<pre><code>index == value
</code></pre>

<p>Can you express that as an int?</p>

<blockquote>
  <p>Just cast the bool to an int.</p>
</blockquote>

<p>What is the definition of a row?</p>

<pre><code>[int(value == index) for index in range(1 + width)]
</code></pre>

<p>What does it look like for all values?</p>

<pre><code>[[int(value == index) for index in range(1 + width)] for value in list_1d]
</code></pre>

<p>Demonstrate:</p>

<pre><code>&gt;&gt;&gt; width=max(list_1d)
&gt;&gt;&gt; [[int(value == index) for index in range(1 + width)] for value in list_1d]
[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]]
</code></pre>

python arrays list numpy transform