for i in result:
for j in i:
In this nested loop, i
iterates over the rows of result
and j
iterates over the cell contents. Thus, in the loop body:
result.append(i*n+j)
this line attempts to multiply a row by n
, add j
(a string) to that, and append the output of that to result
. No part of that is what you want to do. You could fix this by iterating over the indices and assigning to elements instead of appending:
for i in range(m):
for j in range(n):
result[i][j] = i*n+j
but it's easier to build the whole array the way you want it with range
instead of initializing it with garbage and assigning the elements:
def f2(m, n):
return [range(n*i, n*i+n) for i in xrange(m)]
or if you're on Python 3:
def f2(m, n):
return [list(range(n*i, n*i+n)) for i in range(m)]
or if list comprehensions are strange and unreadable to you:
def f2(m, n):
result = []
for i in range(m):
result.append(list(range(n*i, n*i+n)))
return result