在不改变列表中数据排列结构的前提下,找出以下列表中最接近最大值和最小值的平均值 的数
li = [-100,1,3,2,7,6,120,121,140,23,411,99,243,33,85,56]
代码如下:
li = [-100,1,3,2,7,6,120,121,142,23,410,99,243,33,85,56]
max_li = li[0]
mini_li = li[0]
for i in li:
if i > max_li:
max_li = i
if i < mini_li:
mini_li = i
average_valve = (mini_li+max_li)/2
average_li = li[0]
for i in li:
if abs(i - average_valve) < abs(average_li - average_valve):
average_li = i
print(average_li)