Avatar ·

Creating an n-dimensional array of length n

📁 список, python, вопрос, пример

Is it possible to create an n-dimensional array? If so, how can it be implemented? If a matrix is a "list of lists", then it would look something like this:

[  [ [...[]...] ],[ [...[]...] ]  ]

Well, or to make it clearer, like n nested brackets, picture below,

(a1[...[]...]b1, a2_m[...[]...]b2_m,...)

And how to implement this in code? If, say, I need to input n from the keyboard. Many thanks!

Avatar ·

This is not split - it's input throwing an exception. And why is that? Ingratitude. It says the following - input does two things:

1) Reads from stdin.

2) Everything that is read gets executed (eval).

Accordingly, the string 1 2 3 cannot be executed due to invalid syntax. In Py3, the behavior of input() was changed so that people wouldn't get confused anymore. Use . Or Python3. You can also use the library so that when you suddenly migrate to another version, your code won't break, because in Py3 the raw_input() function is missing.

Log in to leave an answer

Blogs