0

// It is just changing values of first row and not the whole table ? can someone tell me how to run loop in whole table

def remove_outliers(table):
     mx = max(map(max, table))
     mn = min(map(min, table))
     avg = (mx + mn) / 2
     for row in table:
         row[:] = [avg if x in (mx, mn) else x for x in row]
         return table


  • Is it answers your question https://stackoverflow.com/questions/16548668/iterating-over-a-2-dimensional-python-list? – OxQ Apr 05 '20 at 09:25

2 Answers2

0

I think your return table is intended wrong. It will return after the first row is processed. Try this:

def remove_outliers(table):
    mx = max(map(max, table))
    mn = min(map(min, table))
    avg = (mx + mn) / 2
    for row in table:
        row[:] = [avg if x in (mx, mn) else x for x in row]
    return table

Lydia van Dyke
  • 2,308
  • 3
  • 11
  • 24
0

The return statement is in the wrong place. The function will exit in the first iteration itself. Un-indent return statement out of for loop. Then it will exit after the complete iteration

for row in table:
    row[:] = [avg if x in (mx, mn) else x for x in row]
return table
Aroo
  • 57
  • 1
  • 10