Python Forum

Full Version: Python Exercise: Generate a two-dimensional array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not a student anywhere, just doing python exercises to try and get better at coding through practice and sheer determination.
Currently, I am doing 100 python challenges that provide a challenge and the answer.  The question I was stuck at stated:

# Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array.
# The element value in the i-th row and j-th column of the array should be i*j. <--- where the problem losses me

The answer provided, I don't understand it. That's why I am here, to see if you guys could explain how the code provides the answer.

Answer:
[list=1]
[*]row_num = int(input("Input number of rows: "))  

[*]col_num = int(input("Input number of columns: "))  

[*]multi_list = [[0 for col in range(col_num)] for row in range(row_num)]  

[*]  

[*]for row in range(row_num):  

[*]    for col in range(col_num):  

[*]        multi_list[row][col]= row*col  

[*]  

[*]print(multi_list)  
[/list]
I was able to rewrite part of the code to:
row= int(input("insert row \n: "))
col = int(input("insert col \n: "))

def d_array(r,c):
   array = [[i*j for j in range(c)] for i in range(r)]
   return array

print(d_array(row,col))
Sadly I still don't understand how it works, anything would help even suggested reading material. I really want to understand
The variable names i, j, r, and c are bad variable names. They are not descriptive. Note how the example use row and col.

So, explaining from the example: multi_list is defined as a two dimensional array of zeros. The first for loop goes through the indices (numbers) of the rows. For five rows it would go through 0, 1, 2, 3, 4. The second for loop does the same thing for the columns. If you had three columns it would go through the indices 0, 1, 2. The statement within the two for loops assigns to the cell in the row for row index and and column for the column index, and assigns that cell the product of the row index and the column index.
(Apr-20-2017, 10:03 PM)ichabod801 Wrote: [ -> ]The variable names i, j, r, and c are bad variable names. They are not descriptive. Note how the example use row and col.

So, explaining from the example: multi_list is defined as a two dimensional array of zeros. The first for loop goes through the indices (numbers) of the rows. For five rows it would go through 0, 1, 2, 3, 4. The second for loop does the same thing for the columns. If you had three columns it would go through the indices 0, 1, 2. The statement within the two for loops assigns to the cell in the row for row index and and column for the column index, and assigns that cell the product of the row index and the column index.

 Thank you for your clarification. I understand the problem alot more and I took your advice on my previous code. I re-wrote it again, this time a little bit different.


import numpy as np

row_num = int(input("Enter number of rows \n: "))
col_num = int(input("Enter number of columns \n: "))
d_array = np.zeros((row_num,col_num))
shape = d_array.shape

for row in range(1,shape[0]):
   for col in range(1,shape[1]):
       d_array[row,col]= row*col

print(d_array)
Your first attempt with nested comprehensions was more "pythonic" (if we forget the variable names)
row_num = int(input("Enter number of rows \n: "))
col_num = int(input("Enter number of columns \n: "))
#d_array = np.zeros((row_num, col_num))--useless
shape = row_num, col_num    #shape = d_array.shape

def d_array(shape):
   array = [[row*col for col in range(shape[1])]for row in range(shape[0])]
   return array

print(d_array(shape))
I keep trying different approaches to see which one is easier for me to understand. 
Thank you for  your time and evaluation.