0

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")
2
  • 1
    I found out that there is nothing such as arrays in python....wow, check this docs.python.org/3.6/library/array.html Commented Aug 7, 2017 at 9:53
  • The core issue with your code is that it tries to alter positions that don't have contents; there's only one row, and it contains no columns. The two answers work around this by first creating a matrix containing 0 in all positions. Commented Aug 7, 2017 at 10:46

2 Answers 2

1
def array_input(row, column):

    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
            print(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)


a, b = 2, 2;
array_list = [[0 for x in range(a)] for y in range(b)] 
array_input(2,2)
array_output(2,2)

This should work for you. You can of course switch the assignment of a and b to the users input. The important part is the assignment of the array_list variable. Hope this ansers your question.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help! Check out Yann Vernier's comment on your question for an explanation on why your solution doesn't work as I left that information out :)
0

This will allow you the most flexibility over what you can do with your array

def array_input(row, column):
    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
            print(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)

def create_array(row,column):
    array_list=[]
    for R in range(0,row):
        array_list.append([])
        for C in range(0,column):
            array_list[R].append(0)
    return 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_list= create_array(a,b)
array_input(a,b)
array_output(a,b)

Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.