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


Messages In This Thread
Python Exercise: Generate a two-dimensional array - by smbx33 - Apr-20-2017, 07:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get the biggest number from a two dimensional list rs74 13 4,239 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Python Exercise Vasanth0910 1 1,943 May-15-2020, 04:41 PM
Last Post: pyzyx3qwerty
  Python exercise HY2000 2 3,266 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,907 Sep-20-2019, 11:56 AM
Last Post: ichabod801
  Two Dimensional Arrays Coder34 1 1,961 Aug-19-2019, 10:48 AM
Last Post: buran
  Python exercise janaraguz 2 2,617 Sep-22-2018, 08:43 PM
Last Post: ichabod801
  Simple exercise - how to write in python tomtom55 6 5,105 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