//它只是更改第一行的值,而不是整个表的值?有人能告诉我如何在全表中运行循环吗
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
我认为您的return 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
返回语句位于错误的位置。函数将在第一次迭代中退出。将返回语句从for循环中取消缩进。然后它将在完成迭代后退出
for row in table:
row[:] = [avg if x in (mx, mn) else x for x in row]
return table