2016-03-16 21 views
1

Python中是否有方法將數組添加到下一個可用空間?所以如果(0,0)已經有一個值,就移動到(0,1)並在那裏添加值。我得到了如下所示,我現在卡住了..有沒有辦法添加到二維數組中的下一個可用空間? (Python 3.x)

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() 
+0

你能說出以另一種方式的問題?你是否試圖找到一種方法來向數組添加值(僅在Python中爲列表),只有當它是空的時纔會這樣? – Igor

+0

你把你的價值放在''(''s中的方式使它們看起來像Tuples。我有點困惑你在問什麼,但我認爲我足夠接近,能夠幫助你做更多的澄清。所需的輸出? – Igor

+0

因此,如果現在使用輸入5輸出此信息,您將得到: ['5',0,0,0,0] [0,0,0,0,0] [0,0 ,0,0,0] [0,0,0,0,0] [0,0,0,0,0] 但我想添加更多的代碼,所以當它再次要求輸入時,它會添加到下一列,然後添加到下一列,在第一行將被填滿後,它將移動到第二行並再次從第一列開始 – Martynas

回答

1

這裏是一個例子。我只是運行循環9次。您應該能夠將其應用到您的OOP樣式代碼中。我也使用標準庫中的pprint模塊,因爲我喜歡它顯示嵌套列表的方式。 (假設5總是進入):循環的最後一次迭代之後

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) 

輸出

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]] 
+0

它幫了很多!如果您有興趣,請做出一些更改並添加其他內容,請參閱下面的內容。謝謝! – Martynas

1
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() 
相關問題