2

Now i just want to replace the min, max value with the x in the table.... and i dont know how to ...

for i in table[1:]:
    mn = min(i) if min(i) < mn else mn 
    mx = max(i) if max(i) > mx else mx
    x = (mn+mx)/2
2
  • 1
    Framing your question in more concise way would increase chances of getting some help. Try describing only the problem you're having and be as clear as possible. No need to state that you are new to python - there's no such thing as dumb question, you're not expected to know everything. Commented Apr 3, 2020 at 14:34
  • Thank you for the tip. I was just a bit scared :) Commented Apr 3, 2020 at 14:42

2 Answers 2

1

The following should work:

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]
    # OR
    for row in table:
        for i, x in enumerate(row):
            if x in (mx, mn):
                row[i] = avg

max(map(max, table)): applies the max function to each row in table, and takes the max of all those "maxes".

row[:] = ...: slice-assignment. This is a mutation on the row object. Simply row = ... would just rebind the loop variable without affecting the list object that is still indexed by table.

[avg if x in (mx, mn) else x for x in row]: general conditional list comprehension. Fairly self-explanatory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, if possible can you explain this part for me: row[:] ?? What does this mean
0

If you are using numpy check out the clip function (https://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.