How to accept 2 dimension array input in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to accept 2 dimension array input in python?

19th Aug 2018, 3:49 AM
Ajay Patil
Ajay Patil - avatar
3 Answers
+ 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.
19th Aug 2018, 4:37 AM
Kirk Schafer
Kirk Schafer - avatar
+ 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😆
20th Aug 2018, 4:43 PM
Flandre Scarlet
Flandre Scarlet - avatar
0
actually it's easy to take 2D inputs in C or C++. 2D array is useful specially for matrix problems
21st Aug 2018, 2:59 PM
Ajay Patil
Ajay Patil - avatar