Scenario, a two-dimensional list with any number of rows and columns, and the program returns a list of the row and column indices of the maximum value in the list. Example
print max_position([[2,4],[6,0],[2,6]])
list visualised:
2,6,2
4,0,6
result be [(1,0),(2,1)]
Now, how can I allow the user to enter any rows or columns of two-dimensional list to work for my code?
The code:
def max_position(list2D):
if not list2D:
return []
else:
maxi = list2D[0][0]
pos = [] #initialise with no position
for row in range(len(list2D)):
for col in range(len(list2D[row])):
if (list2D[row][col] == maxi):
if (row,col) != (0,0):
pos.append((row,col))
elif (list2D[row][col] > maxi): # new maximum found
pos = [(row,col)]
maxi = list2D[row][col]
return pos