Python 输入列表的中间项
我需要取3个数字,打印最大值、最小值和中间值。以下是我写的:Python 输入列表的中间项,python,Python,我需要取3个数字,打印最大值、最小值和中间值。以下是我写的: new_list = [] for i in range(3): new_list.append(int(input("Enter a number: "))) x = min(new_list) y = max(new_list) z = print("The max is %d, the middle is %d and the min is é %d." % (y, x , z)) 但我不知道如何定义z,所以它
new_list = []
for i in range(3):
new_list.append(int(input("Enter a number: ")))
x = min(new_list)
y = max(new_list)
z =
print("The max is %d, the middle is %d and the min is é %d." % (y, x , z))
但我不知道如何定义z,所以它可以是中间项。有什么帮助吗?只需对它们进行排序:
x, z, y = sorted(new_list)
只需对它们进行排序:
x, z, y = sorted(new_list)
您也可以使用列表理解来完成
new_list = []
for i in range(3):
new_list.append(int(input("Enter a number: ")))
x = min(new_list)
y = max(new_list)
z = [i for i in new_list if i!=x and i!=y][0]
print("The max is %d, the middle is %d and the min is é %d." % (y, z , x))
您也可以使用列表理解来完成
new_list = []
for i in range(3):
new_list.append(int(input("Enter a number: ")))
x = min(new_list)
y = max(new_list)
z = [i for i in new_list if i!=x and i!=y][0]
print("The max is %d, the middle is %d and the min is é %d." % (y, z , x))
按值排序,获取中间值按值排序,获取中间值