使用Python打印Min1和Min2 [英] Printing Min1 and Min2 using Python

查看:141
本文介绍了使用Python打印Min1和Min2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的代码中缺少什么,以便将min1和min2设置为两个最小数字?

What am I missing in this code here so that it sets min1 and min2 to the two smallest numbers?

def test() : # do not change this line!
  list = [4, 5, 1, 9, -2, 0, 3, -5] # do not change this line!          
  min1 = list[0]
  min2 = list[1]
  #missing code here
  print(min1, min2)
  return (min1, min2) # do not change this line!
  # do not write any code below here  
test() # do not change this line!
  # do not remove this line!

推荐答案

List不会对其中的元素进行隐式排序.您已经使用sort()函数对列表进行了排序.因此,在从列表中获取min1和min2之前,您已经使用sort函数获得了排序列表,可以使用 nameofyourlist .sort()进行此操作,并且您也不会将test()的返回值分配给任何变量,因此您可以避开那条线.您的最终代码可以如下所示

List does not sort elements in it implicitly. You have sort the list with sort() function. So before you take min1 and min2 from list you have sort list using sort function, you can do that with nameofyourlist.sort() and also you are not assigning return value of test() to any variable so you can avoid that line. Your final code can be as below

def test():
    list = [4, 5, 1, 9, -2, 0, 3, -5]
    list.sort()
    min1 = list[0]
    min2 = list[1]
    print(min1, min2)

test()

这篇关于使用Python打印Min1和Min2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆