python matrix creation without using numpy or anything and max sum of rows and columns
<p>Want to create a matrix like below in python</p>

<pre><code>[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
</code></pre>

<p>but not sure how to do, i tried list comprehension like below</p>

<pre><code>[[y+x for y in range(4)] for x in range(4)]
</code></pre>

<p>which gives output like below</p>

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

<p>and also want print the row with maximum sum and columns with maximum sum.</p>

<p>Thanks in advance </p>

<p>The range function accepts 3 arguments <code>start</code>, <code>end</code> and <code>step</code>. You can use one range with step 4 and another one for creating the nested lists using the outer range.</p>

<pre><code>&gt;&gt;&gt; [[i for i in range(i, i+4)] for i in range(1, 17, 4)]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
</code></pre>

<p>here is another way with one <code>range</code>:</p>

<pre><code>&gt;&gt;&gt; main_range = range(1, 5)
&gt;&gt;&gt; [[i*j + (i-1)*(4-j) for j in main_range] for i in main_range]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
</code></pre>

<p>And here is a Numpythonic approach:</p>

<pre><code>&gt;&gt;&gt; n = 4
&gt;&gt;&gt; np.split(np.arange(1, n*n + 1), np.arange(n ,n*n, n))
[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([ 9, 10, 11, 12]), array([13, 14, 15, 16])]
</code></pre>

python matrix