Python Forum

Full Version: Finding and indexing maximum value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a large grid, but let's say it is 4 cells wide and 3 cells high. Index of the first cell is 0 and it starts bottom left, finishes in the upper right corner with index 11.
Each cell has a value. So we have a df:

cell;value
0;8
1;2
2;1
3;6
4;4
5;6
6;7
7;1
8;2
9;5
10;3
11;5

How do I identify cell that has a max value around itself?

- For corner cells this means that we look at the observed cell and all three around it.
- For middle cells we observe the cell and all 8 around it.

The goal is to find the cell with max value, add to df a new column and to the cell with max value return 1 and all the others 0.

For the upper example this would mean:

cell;value;maxValue
0;8;1
1;2;0
2;1;0
3;6;0
4;4;0
5;6;0
6;7;1
7;1;0
8;2;0
9;5;0
10;3;0
11;5;0

EDIT:
This is the "image" of the grid and its values, respectively.

IMAGE LINK

Anyone? Should I use IF command and go with if (i+1) and (i-1) and ... ?
I would come up with a list of possible offsets. Loop through the cells. For each cell, loop through the offsets, and get the other cells at those offsets, adding them up. You'll need a check to make sure you're not checking outside the matrix (or a try/except block). Keep track of the highest total and what it's index is as you go along.
(Oct-24-2018, 03:00 PM)ichabod801 Wrote: [ -> ]I would come up with a list of possible offsets. Loop through the cells. For each cell, loop through the offsets, and get the other cells at those offsets, adding them up. You'll need a check to make sure you're not checking outside the matrix (or a try/except block). Keep track of the highest total and what it's index is as you go along.

Implemented on matrices it will look something like that:

def maximum_around(matrix, r, c):
    """Find maximum value with indices around value in matrix."""
    
    values = list()
    coordinates = list()
    
    offset = (0, 1, -1)
    indices = ((i, j) for i in offset for j in offset)
    
    next(indices, None)
    
    for rec in indices:
    
        try:
            row = r - 1 + rec[0]
            col = c - 1 + rec[1]
            
            if 0 <= row and 0 <= col:
                value = matrix[row][col]
                values.append(value)
                coordinates.append([row, col])
    
        except IndexError: 
            continue
        
    return {max(values): coordinates[values.index(max(values))]}
  • offsets defines relative changes needed for given indice to get surrounding elements (incl position of given indice himself because relative change [0][0] results in no change in indice)
  • due to order of elements in offset, indices generator object created in such way that first item is (0, 0). First item is consumed by next()
  • looping through generator object remaining elements, calculating and assigning values to matrix indices and adding values and coordinates to lists if indices are not negative (we don't want have row[-1]) and continuing when indices are out of range (IndexError)
  • function expects that user will enter cell location in finger-based index and deduction -1 is needed for converting values to 0-based index. It can be easily changed.

>>> n = [[-1, -2, -3],
         [-4, -5, -6],
         [-7, -8, -9]
        ]

>>> maximum_around(n, 3, 3)
{-5: [1, 1]}