Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在列表中找到最高值和最低值?_Python_List - Fatal编程技术网

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

Python 如何在列表中找到最高值和最低值?,python,list,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 Scor

我有一个类似于学校的代码:

 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
打印(“耦合结果”)
对联=[]
法官=5
而len(coupleA)<评委:
判断1=输入(“第1轮夫妇A的输入分数:”)
判断2=输入(“第1轮A对夫妇的输入分数:”)
判断3=输入(“第1轮A对夫妇的输入分数:”)
判断4=输入(“第1轮夫妇A的输入分数:”)
判断5=输入(“第1轮夫妇A的输入分数:”)
对联A.附加(判决1)
对联A.附加(判决2)
对联A.附加(判决3)
对联A.附加(判决4)
对联A.附加(判决5)
打印对联
打印(“\n这是A对夫妇的分数:”)
打印对联

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

不用对所有值进行排序,这可能会在计算上很昂贵,您可以从所有值中减去最高值和最低值

# 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])
int_coupleA = [int(i) for i in coupleA]
between = sum(int_coupleA) - max(int_coupleA) - min(int_coupleA)

print(coupula中i的最大值(int(i))和coupula中i的最小值(int(i))
while循环的目的是什么?由于
judges=5
并且您在每个循环中添加了5个内容,因此
while
循环应该只执行一次。@FilipMłynarski抱歉,如果我没有说清楚,但是我希望添加介于最高值和最低值之间的值。例如,如果值为1,2,3,4,5,我希望添加2,3,4?谢谢,这正是我想要的!很高兴帮助你!有不止一对,比如coupleB,coupleC等等,我也找到了中间数的和,现在我需要找到所有新值中的最高值!XD您可以找到列表的最大值和最小值,正如您在其他示例中使用min()和max()所看到的。我对python不太在行,我的大脑在经历了这一切之后很痛苦,但感谢您的评论!