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
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.
Sign up to join this communityIs 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
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.