I've recently started learning python and have stumbled in a syntax dilemma. Is there a way to do what this pice of code does:
def crescentOrderArray1(rows, columns):
arr = [[] for i in range(rows)]
count = 1
for row in range(rows):
for colum in range(columns):
arr[row].extend([count])
count = count 1
return arr
with a discreet syntax like
def crescentOrderArray2(rows, columns):
count = 1
arr = [[count for i in range(rows)] for i in range(rows)]
return arr
I've tried modifying the "count" statement in "crescentOrderArray2", but with no success. I was expecting a way iterate each time it adds a "count"
CodePudding user response:
Use 2 range objects. One iterates the start count for each row and the other expands into its column values.
def crescentOrderArray3(rows, columns):
return [list(range(i, i columns))
for i in range(1, rows*columns 1, columns)]
CodePudding user response:
Your count
variable does not increment. Fortunately, you can just use the i and j values to calculate what count should be. See below output.
def crescentOrderArray1(rows, columns):
arr = [[] for i in range(rows)]
count = 1
for row in range(rows):
for colum in range(columns):
arr[row].extend([count])
count = count 1
return arr
def crescentOrderArray2(rows, columns):
arr = [[j 1 cols*i for j in range(columns)] for i in range(rows)]
return arr
rows,cols = (3,5)
print(crescentOrderArray1(rows,cols))
print(crescentOrderArray2(rows,cols))
>>>[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
>>>[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
CodePudding user response:
No, in the code that you've written the count
variable on the first line is different from the count
variable on the second line.
The second line uses a local variable count
within the list comprehension expression and this nested count
shadows the one defined on the first line.
def crescentOrderArray2(rows, columns):
count = 1
arr = [[count for i in range(rows)] for i in range(rows)]
return arr
One possible way you can rewrite your code into a more idiomatic form is to use list comprehensions and a helper function chunks
that splits a list into equally sized chunks:
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i n]
def crescentOrderArray2(rows, columns):
return [list(x) for x in chunks(range(1, rows*columns), columns)]
CodePudding user response:
Your count
variable isn’t updated inside your list comprehension. Just use the values from the range generators:
arr = [[row*cols col for col in range(1, cols 1)] for row in range(rows)]
This will produce a 2D array of size rows*cols
, with the top left value at zero and then incrementing by one each cell going right.
But there’s no real reason to do it in as few lines as possible. Write the code so it’s readable.
CodePudding user response:
try this, it's the most compact way:
def crescent_order_array(rows, columns):
return [[i j for i in range(1, columns 1)] for j in range(0, rows*columns, columns)]