I am trying to make a function to input and one to output a 2D array . After some research (While working with 1D array) I found out that there is nothing such as arrays in python. However, I could achieve my goal using lists.
The following code worked for 1D array using list:
def array_input(num):
for Index in range(0, num):
ind = int(input("Please enter element {0} : ".format(Index)))
array_list.append(ind)
def array_output():
for Index in range(0, len(array_list)):
print("Element {0} is {1} ".format(Index, array_list[int(Index)]))
"""print(array_list)"""
array_list = []
a = int(input("Please enter the length of the array"))
array_input(a)
array_output()
input("Pres any key to continue")
The following is what I wrote for 2D arrays using list of lists: output is working, however input is not . Can anyone help me with figuring out how i can add to the lists of lists new elements (kind of like a 2D matrix)?
def array_input(row, column):
print(array_list)
for R in range(0, row):
for C in range(0, column):
ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
array_list[R][C] = ind
def array_output(row, column):
for R in range(0, row):
for C in range(0, column):
print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
print(array_list)
array_list = [[]]
a = int(input("Please enter the number of rows of the array"))
b = int(input("Please enter the number of columns of the array"))
array_input(a, b)
array_output(a, b)
input("Pres any key to continue")