1
\$\begingroup\$

Is there any way I can make this function more inline?

def nDimensionalArray(n, s):
 x = 0
 for i in range(n):
 x = [x for j in range(s[i])]
 return x
ferada
11.4k25 silver badges65 bronze badges
asked Feb 17, 2016 at 10:17
\$\endgroup\$
1
  • \$\begingroup\$ Cross-posted from Stack Overflow \$\endgroup\$ Commented Feb 24, 2016 at 2:43

1 Answer 1

3
\$\begingroup\$

You didn't write a docstring (which you should have done) or explain your function (which you should also have done), but it looks like n is supposed to be the same as the length of s. If so, why should n have to be specified?

Your function has some unintuitive behaviour:

>>> matrix = nDimensionalArray(3, [1,2,3])
>>> matrix
[[[0], [0]], [[0], [0]], [[0], [0]]]
>>> matrix[0][0][0] = 1
>>> matrix
[[[1], [1]], [[1], [1]], [[1], [1]]]

Changing one element changes everything! I assume that that is unintentional. If it is intentional, then it needs to be clearly documented.

answered Feb 17, 2016 at 11:08
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.