Change language

Accept matrix input from user in Python

Examples :

  // 3 x 4 matrix  1 2 3 4 M = 4 5 6 7 6 7 8 9  / / 2 x 3 matrix in Python  A = ([2, 5, 7], [4, 7, 9])  // 3 x 4 matrix in Python where entries are floating numbers  B = ([1.0, 3.5, 5.4, 7.9], [9.0, 2.5, 4.2, 3.6], [1.5, 3.2, 1.6, 6.5]) 

input. Some of the methods for the user input matrix in Python are shown below:

Code # 1:

# Base matrix input code from user

 

R = int ( input ( "Enter the number of rows:" ))

C = int ( input ( " Enter the number of columns: " ))

 
# Initializers set matrix

matrix = []

print ( "Enter the entries rowwise: " )

  
# For user input

for i in range (R):  # A for loop for row entries

a = []

for j in range (C):  # A for loop to write this in columns

a.append ( int ( input ()))

matrix.append (a)

 
# To print the matrix

for i in range (R):

for j in range (C):

print (matrix [i] [j], end = " " )

print ()

Output:

 Enter the number of rows: 2 Enter the number of columns: 3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 

One liner:

# one-line logic for inputting rows and columns

mat = [[ int ( input ()) for x in range (C)] for y in range (R)]

Code # 2: Using map () and Numpy .

Python has a popular library NumPy . This library is a fundamental library for any scientific calculations. It is also used for multidimensional arrays, and since we know the matrix — this is a rectangular array, we will use this library for the user input matrix.

import numpy as np

 

R = int ( input ( " Enter the number of rows: " ))

C = int ( input ( "Enter the number of columns:" ))

 

 

print ( "Enter the entries in a single line (separated by space):" )

 
# User input of posts in
# one line separated by space

entries = list ( map ( int , input (). split ()))

 
# To print a matrix

matrix = np.array (entries) .reshape (R, C)

print (matrix) < / code>

Exit:

 Enter the number of rows: 2 Enter the number of columns: 2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 ​​2] [3 1]] 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically