Is there a way to add to a next free space in a two-dimensional array? (Python 3.x)

Is there a way in Python to add to an array to the next free space available? So if (0,0) has already a value, move on to the (0,1) and add the value there. I got as far as shown below, and I'm stuck now..

class Array:

    def __init__(self):
        self.array = [[0 for x in range(5)] for x in range(5)]

    def view(self):
        for x in self.array:
            print(x)

    def input(self):
        item = input("Enter an item: ")
        self.array[0][0] = item

array = Array()

array.input()
array.view()

Answers


Here is one example. I am simply running the loop 9 times. You should be able to work it into your OOP style code. I am also using pprint module from standard library as I like how it displays nested lists.

from pprint import pprint as pp


myList = [[0 for x in range(5)] for x in range(5)]

for i in range(9):
    userInput = int(input("Enter an item: "))
    isFound = False # This flag is used to prevent duplicate entries.

    for rowIndex, row in enumerate(myList):
        for colIndex, column in enumerate(row):
            if column == 0 and not isFound:
                myList[rowIndex][colIndex] = userInput
                isFound = True
                break
    pp(myList)

Output after last iteration of the loop: (assuming 5 was always entered):

Enter an item: 5
[[5, 5, 5, 5, 5],
 [5, 5, 5, 5, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

class Array:

    def __init__(self):
        self.myList = [[0 for x in range(5)] for x in range(5)]

    def input(self):
        print("You will be asked to put a value, if you want to stop, press RETURN key.")
        for i in range(25):
            print()
            userInput = input("Enter an item: ")
            isFound = False


            if userInput == '':
                menu()

            for rowIndex, row in enumerate(self.myList):
                for colIndex, column in enumerate(row):
                    if column == 0 and not isFound:
                        self.myList[rowIndex][colIndex] = userInput
                        isFound = True
                        break


            print()
            for x in self.myList:
                print(x)

    def remove(self):
        print("Which box do you want to delete? Type in coordinates.")
        x = int(input("Please enter a column: "))
        x -= 1
        y = int(input("Please enter a row: "))
        y -= 1
        self.myList[x][y] = 0
        print()
        for i in self.myList:
            print(i)
        menu()

    def view(self):
        for i in self.myList:
            print(i)
        menu()



array = Array()

def menu():
    print()
    print()
    print()
    print("****************")
    print("      MENU")
    print("****************")
    print("Please choose:")
    print("1. Add an item to the array")
    print("2. Remove an item from the array")
    print("3. View array")
    print()
    print("0. Quit")
    print("****************")
    option = int(input("Enter (1, 2 or 0): "))
    print()
    if option == 1:
        array.input()
    elif option == 2:
        array.remove()
    elif option == 3:
        array.view()
    elif option == 0:
        quit()
    else:
        print("Error..")


menu()

Need Your Help

Is newsstand framework required for creating newsstand app?

ios newsstand-kit

This question may seem strange, but still would like to clarify for myself. Is Newsstand framework required to use in newsstand app? I mean, can I make own issue model instead of NKIssue, download ...

Python PIL struggles with uncompressed 16-bit TIFF images

python python-imaging-library tiff

My system is Mac OS X v10.8.2. I have several 2560x500 uncompressed 16-bit TIFF images (grayscale, unsigned 16-bit integers). I first attempt to load them using PIL (installed via Homebrew, versi...