Reputation: 27
i'm trying to read the input from the standard input (console) but my code is only reading the first number in the line
x=input("enter the matrix?")
print(read_matrix(x))
def read_matrix(matrix):
list_of_lists = []
for line in matrix:
new_list = [int(elem) for elem in line.split()]
list_of_lists.append(new_list)
return list_of_lists
Upvotes: 0
Views: 4660
Reputation: 25100
From your example code it seems that you're using a list of lists to aggregate your data... An infinite loop with something to break out can generate such a data structure --- to break the input loop I've used a try-except clause, because it seems to me the most natural thing to use an EOF to signal the end of input, but you can use any other possible mechanism.
In [14]: def read_matrix():
print('Input 1 row per line, with numbers separated by spaces.\n',
'Use a C-d to stop input.')
matrix = []
while True:
try:
matrix.append([int(n) for n in input().split()])
except EOFError:
return matrix
....:
In [15]: read_matrix()
Input 1 row per line, with numbers separated by spaces.
Use a C-d to stop input.
1 2 3
4 5 6
Out[15]: [[1, 2, 3], [4, 5, 6]]
of course I typed a C-d (on a new line, that was eaten by the interpreter) to stop the input loop.
The function is overly compact, you may prefer to unfold the elements of the list comprehension and possibly add some consistency check (number of columns maybe?) before updating matrix
In [18]: def read_matrix(nc):
print('Input 1 row per line, with %d numbers separated by spaces.\n'%nc,
'Use a C-d to stop input.')
matrix = []
while True:
try:
line = input()
numbers = [int(n) for n in line.split()]
if not len(numbers) == nc:
print('Wrong number of columns, repeat this line')
continue
matrix.append(numbers)
except EOFError:
return matrix
....:
In [19]: read_matrix(3)
Input 1 row per line, with 3 numbers separated by spaces.
Use a C-d to stop input.
1 2 3
4 5 6 7
Wrong number of columns, repeat this line
4 5 6
Out[19]: [[1, 2, 3], [4, 5, 6]]
Upvotes: 0
Reputation: 518
Assuming that you are using Python 2.7, the problem lies in the fact that you are using input
to process the input typed by the user. Indeed, if you look at the related documentation (https://docs.python.org/2/library/functions.html#input), you can see that basically input
corresponds to eval(raw_input())
, which means that the expression argument is parsed and evaluated as a Python expression.
If you want to read the standard input as a string, you should use raw_input
instead.
If you are using python 3, then input
is fine.
Notice, however, that you should format the input string in such a way that read_matrix
can recognize the elements in the same row, and when a new row should be added.
One possible solution (that works with python 2.7), keeping your structure of the code and assuming that the format of the input is having numbers in the same row separated by a space, and rows separated by ';' (e.g., 1 2 3; 4 5 6; 7 8 9), is:
def parse_numbers_list(formatted_string):
list_of_lists = [map(int, row.split()) for row in formatted_string.split(';')]
return list_of_lists
x = raw_input("enter the list of lists of numbers?")
print(parse_numbers_list(x))
For python 3, similarly:
def parse_numbers_list(formatted_string):
list_of_lists = [list(map(int, row.split())) for row in formatted_string.split(';')]
return list_of_lists
x = input("enter the list of lists of numbers?")
print(parse_numbers_list(x))
Notice that I changed the name of the function, as generally, the code does not check whether the input string complies with the matrix constraint -- i.e., same number of columns for every row. You can add a check to see whether every list has the same size.
Also, a complete solution should include a check on whether the input string is well-formatted.
There are of course a plethora of other ways to implement code to read a matrix from standard input, including using external libraries, e.g., numpy, but I think, as you are learning Python, it is good that you are starting with the basics.
Upvotes: 2
Reputation: 6398
Your code has two problems. First is that you do not have a way of telling cells apart. If someone enters 112 for the first row, you interpret it as [1],[1],[2] when they could have meant any of a number of things. To fix this line 4 can be changed to
new_list = [int(elem) for elem in line.split(',')]
This will cause it to split cells by commas. The second issue is that pressing enter tells the interpreter that you are done entering input. There are two solutions to this. You could have users input matrix dimensions and use a for loop to let them enter each row, or you could use Matlab's syntax where a 2x2 matrix is entered as [[a,b],[c,d]].
Upvotes: 0