如何在列表中找到最高和最低值?

时间:2018-11-26 19:34:51

标签: python list

我有一个看起来像这样的学校代码:

 print ("Couple A Results")
coupleA = []
judges = 5
while len(coupleA) < judges:
    judge1 = input("Input Scores For Couple A in round 1: ")
    judge2 = input("Input Scores For Couple A in round 1: ")
    judge3 = input("Input Scores For Couple A in round 1: ")
    judge4 = input("Input Scores For Couple A in round 1: ")
    judge5 = input("Input Scores For Couple A in round 1: ")
    coupleA.append(judge1)
    coupleA.append(judge2)
    coupleA.append(judge3)
    coupleA.append(judge4)
    coupleA.append(judge5)
    print coupleA
print ("\nThese are the scores for couple A:")
print coupleA

我想用另一段代码从输入的列表中提取最高和最低值,并在两者之间添加值。有谁知道我会怎么做?谢谢

2 个答案:

答案 0 :(得分:3)

# convert to int because input returns string
values =(int(x) for x in coupleA)

# sort the values
values_sorted = sorted(values)

# sum all values except the first and the last one
sum(values_sorted[1:-1])

答案 1 :(得分:0)

您可以从所有值中减去最高和最低值,而不是对所有值进行排序,这可能会导致计算量大。

int_coupleA = [int(i) for i in coupleA]
between = sum(int_coupleA) - max(int_coupleA) - min(int_coupleA)