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
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.
If you are using numpy
check out the clip
function ( https://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.