Python Forum
Python Exercise: Generate a two-dimensional array
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Exercise: Generate a two-dimensional array
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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)
Reply
#4
Your first attempt with nested comprehensions was more "pythonic" (if we forget the variable names)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get the biggest number from a two dimensional list rs74 13 4,031 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Python Exercise Vasanth0910 1 1,891 May-15-2020, 04:41 PM
Last Post: pyzyx3qwerty
  Python exercise HY2000 2 3,190 Nov-07-2019, 08:01 AM
Last Post: HY2000
  How to Generate and Print An Array with Random Numbers in Python in the johnnynitro99293 14 9,560 Sep-20-2019, 11:56 AM
Last Post: ichabod801
  Two Dimensional Arrays Coder34 1 1,895 Aug-19-2019, 10:48 AM
Last Post: buran
  Python exercise janaraguz 2 2,568 Sep-22-2018, 08:43 PM
Last Post: ichabod801
  Simple exercise - how to write in python tomtom55 6 4,968 Sep-28-2017, 07:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020