Read k matrices size nxm from stdin in Python
<p>I need to read k matrices size nxm from stdin in Python.
In the first line there must be the number of matrices (k), and then - k descriptions of matrices: in the first line 2 integer numbers for size (n and m) devided by space, then the matrix.</p>

<p>Here's an example:</p>

<pre><code>2
2 3
4 5 6
3 1 7
4 4
5 3 4 5
6 5 1 4
3 9 1 4
8 5 4 3
</code></pre>

<p>Could you please tell me how I can do this?
I could have done this only without considering m (for 1 matrix):</p>

<pre><code>n = int(input()) 
a = []
for i in range(n):
    a.append([int(j) for j in input().split()])
</code></pre>

<p>I have found some similar questions but stdin is not used (e.g. reading from file is used) or the size of string in matrix is not set.</p>

<p>You are on a right way. try to break it in simple steps. Basically a  n X m  matrix is n rows and each row would have m elements (pretty obvious). what if we have n=1 then we have a line in which there would be m element. to take such input we would just </p>

<pre><code>matrix = input().split()  #read the input
matrix = [ int(j) for j in matrix]   #matrix is now 1 x m list
</code></pre>

<p>or simplify this</p>

<pre><code>matrix = [ int(j) for j in input().split() ]
</code></pre>

<p>now suppose we have n rows, that means we have to do this n times which is simply looping n times ,</p>

<pre><code>matrix = [ [ int(j) for j in input().split() ] for i in n ]
</code></pre>

<p>a more pythonic way is using map,</p>

<pre><code>matrix= [ list( map(int, input().split() ) ) for i in range(n) ] 
</code></pre>

python matrix stdin