0
How to accept 2 dimension array input in python?
3 Respuestas
+ 1
A risky way to do it, eval:
# user pastes [[0,1],[2,3]]...you hope
myList = eval(input())
Safer user input*:
0 1 (enter)
2 3
Start with a master list that is empty : []
Then read a line:
0 1
Convert that to a list [0,1], and append it to the master list.
Repeat until end of input.
* This is plain, others may have more elegant ideas.
+ 1
Before answering this question, I think there's another question: Why is it necessary to get an 2-dimension array input?
For any languages, inputs are just strings. Any numbers, arrays or even objects are converted by the programmer in the code. That is to say, you should think of an easy way to get input, which will be also easy to be converted to what you need.
As Kirk Schafer has suggested, get 1-D arrays one by one is much easier than to get directly a 2-D array.
Well, this is a good question, because I've never thought about it until I see this post😆
0
actually it's easy to take 2D inputs in C or C++. 2D array is useful specially for matrix problems