user7274404
user7274404

Reputation:

2D array in python using list of lists

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")

Upvotes: 0

Views: 1739

Answers (2)

Elie
Elie

Reputation: 1

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!

Upvotes: 0

Claudio Brasser
Claudio Brasser

Reputation: 511

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.

Upvotes: 1

Related Questions