I am very new to Python and struggling to find a way to enter a matrix of data. What I need to do is:
- Ask user how many rows
- Ask user how many columns 3 Enable user to type in 1 or 0 values for the specified row x column matrix
I'm actually trying to replicate what was previously undertaken in an old PCW BASIC program! In that version, the user was asked to type a 1 or 0 individually, i.e for a 3 x 3 matrix they would be asked to enter Row 1, Col 1? [Enter] Row 1, Col 2 [Enter] Row 1, Col 3 [Enter] Row 2, Col 1 [Enter] .... etc.
Ideally it would be better if the user was able to type one complete row at a time, i.e 1 0 1 [Enter] 1 1 0 [Enter] 0 1 0 [Enter] or even better in a spreadsheet-like grid but I suspect that is too ambitious for someone just starting out in Python. For each matrix the user would first have entered row and column title labels so it would be preferable to show these in the screen output once the data has been entered.
Beyond this stage there is a series of fairly basic mathematical operations to be completed on the data.
From my initial reading I initially got the impression that I might need to use NumPy or pandas? I have read a bit about these but can't see anything which fairly closely resembles what I am trying to achieve. Now I'm beginning to think that Lists may suffice?
If anyone can help point me in the right direction or give me a few pointers that would be much appreciated.
To illustrate what I'm trying to replicate, this is an extract from the original BASIC code:
1100 INPUT "HOW MANY ELEMENTS (COLUMNS) HAVE YOU"J
1110 PRINT
1120 INPUT "HOW MANY VARIABLES (ROWS) HAVE YOU"I
1130 PRINT
1140 PRINT "INPUT DATA,ROW BY ROW,AS FOLLOWS:"
1150 PRINT "TYPE '0' FOR A VOID"
1160 PRINT "TYPE '1' FOR AN INCIDENT"
1170 PRINT
1180 FOR R = 1 TO I
1190 FOR C = 1 TO J
1200 PRINT "ROW "R;": COLUMN "C;
1210 INPUT "INCIDENT OR VOID"R%(R,C)
1220 NEXT C
1230 NEXT R
Thanks very much!
Robert
2 Answers 2
You could ask the user to enter the rows one by one and finish with an empty row (without asking for the number of rows before):
print ('Enter your row one by one (pressing enter between each row). To terminate, enter an empty row.')
M = []
while True:
r = input ('Next row > ')
if not r.strip (): # Empty input
break
M.append(list(map(int, r.split())))
print('M =', M)
Result:
Enter your [...] empty row.
Next row > 0 1 0
Next row > 1 1 0
Next row > 0 0 1
Next row >
M = [[0, 1, 0], [1, 1, 0], [0, 0, 1]]
numpy is a scientific library which aims at enabling « MATLAB like » code in python (faster manipulation of matrix, and much more). Depending on what you want to do with your matrix, using numpy may not be the fastest way.
A little example (using the matrix M defined before, and regarding your comment):
>>> import numpy
>>> a = numpy.array (M)
>>> a
array([[0, 1, 0],
[1, 1, 0],
[0, 0, 1]])
>>> a.sum (0) # Sum over the first axis (row)
array([1, 2, 1])
>>> a.sum (0) % 2 # Sum + Modulo over the result
array([1, 0, 1], dtype=int32)
>>> b = 1 - a.sum (0) % 2 # Sum + Modulo + Inverse 1 and 0
>>> b
array([0, 1, 0], dtype=int32)
>>> a == b # check for each row
array([[ True, True, True],
[False, True, True],
[ True, False, False]], dtype=bool)
>>> (a == b).all (1) # Check for each raw if all column are True
array([ True, False, False], dtype=bool)
2 Comments
numpy would be useful in your case. With numpy you can do global operation on your matrix like summing over a column, adding (or removing) a specifing value to each cell, etc. I will add a little example at the end of my answer.for i in range(0,columns):
for j in range(0,rows):
A[i][j] = int(input("Enter the value at row %d , column %d : "%(j,i)))
This is the basic prototype for what you want, change the names and functionality as needed.
To print :
for i in range(0,columns):
for j in range(0,rows):
print("The value at row %d , column %d is : %d : "%(j,i,A[i,j]))