【问题标题】:How to find the maximum number in a list using a loop?如何使用循环查找列表中的最大数量?
【发布时间】:2013-01-21 22:54:48
【问题描述】:

所以我有这个列表和变量:

nums = [14, 8, 9, 16, 3, 11, 5]

big = nums[0]

spot = 0

我对如何真正做到这一点感到困惑。我想用这个练习给我一个开端。我如何在 Python 上做到这一点?

【问题讨论】:

    标签: python list loops max


    【解决方案1】:

    通常,您可以使用

    max(nums)
    

    如果您明确想要使用循环,请尝试:

    max_value = None
    for n in nums:
        if n > max_value: max_value = n
    

    【讨论】:

    • 如果列表中的号码是-2怎么办?
    • 为循环解决方案添加一些解释:要获得最大的数字,您遍历列表并记住迄今为止遇到的最大数字。这样一来,当你到达终点时,你就会记住其中最多的人。
    • @alex 可以设置max = nums[0]; max 也不是最好的变量名。
    • 当然,max 这个名字选得不好。 -1 初始值失败,如果列表中的所有数字都
    • (int) > None 将引发 TypeError。
    【解决方案2】:

    给你...

    nums = [14, 8, 9, 16, 3, 11, 5]
    
    big = max(nums)
    spot = nums.index(big)
    

    这将是实现这一点的 Pythonic 方式。如果要使用循环,则以当前最大值循环,并检查每个元素是否较大,如果是,则分配给当前最大值。

    【讨论】:

    • 但没有循环 - 至少没有明确
    • @TheodrosZelleke for i in range(len(list)): # for loop iterates through it。这是在列表长度内重复某些内容的明确方式。这将适用于索引;如果你想做 x 次,range() 'end' 参数应该是 x + 1。所以要做 x 次 x 是列表长度的事情,执行:for i in range(len(list) + 1):。跨度>
    • @F3AR3DLEGEND,感谢您的澄清——但是,我的评论是一种“批评”,而不是一个问题。
    • for i in nums: return max(nums) :p
    • @F3AR3DLEGEND list 是一个内置函数,最好不要将其用作变量名。你最好做for i, _ in enumerate(seq)而不是range(len(seq))
    【解决方案3】:
    nums = [14, 8, 9, 16, 3, 11, 5]
    
    big = None
    
    spot = None
    
    for i, v in enumerate(nums):
        if big is None or v > big:
             big = v
             spot = i
    

    【讨论】:

    • 对于奖励积分,在分配big = nums[0]之前确保nums不为空,如果是,则引发异常(ValueError将是一个不错的选择)。
    【解决方案4】:

    为什么不简单地使用内置的max() 函数:

    >>> m = max(nums)
    

    顺便说一下,类似问题的一些答案可能会有用:

    【讨论】:

    • OP 明确声明他想要一个循环。
    【解决方案5】:

    要解决您的第二个问题,您可以使用for 循环:

    for i in range(len(list)):
        # do whatever
    

    您应该注意,range() 可以有 3 个参数:startendstep。 Start 是从哪个数字开始(如果未提供,则为 0);开始是包容性的。结束是结束的地方(必须给出); end 是独家的:如果你做range(100),它会给你0-99。步长也是可选的,它表示使用什么间隔。如果未提供 step,则为 1。例如:

    >>> x = range(10, 100, 5) # start at 10, end at 101, and use an interval of 5
    >>> x
    [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] # note that it does not hit 100
    

    由于end 是独占的,要包含 100 个,我们可以这样做:

    >>> x = range(10, 101, 5) # start at 10, end at 101, and use an interval of 5
    >>> x
    [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100] # note that it does hit 100
    

    【讨论】:

      【解决方案6】:

      Python 已经为这种需求提供了内置函数。

      list = [3,8,2,9]
      max_number = max(list)
      print max_number # it will print 9 as big number
      

      但是,如果您找到经典 vay 的最大数量,您可以使用循环。

      list = [3,8,2,9]
      
      current_max_number = list[0]
      for number in list:
          if number>current_max_number:
              current_max_number = number
      
      print current_max_number #it will display 9 as big number
      

      【讨论】:

        【解决方案7】:

        对于列表代码 HS 中的最大值,我已经设法让大部分自动分级机使用此代码为我工作:

        list = [-3,-8,-2,0]
        
        current_max_number = list[0]
        for number in list:
            if number>current_max_number:
                current_max_number = number
        
        print current_max_number
        
        def max_int_in_list():
            print "Here"
        

        我不确定 max_int_in_list 的去向。它需要恰好有 1 个参数。

        【讨论】:

        • 我不确定 max_int_in_list 的去向。它需要恰好有 1 个参数。 -- 您是在回答这个问题,还是提出一个新的相关问题? Stackoverflow 是not a discussion forum,它是一个问答网站,所以如果您需要回答后续问题,您应该delete 这个答案和ask here,可能会链接回这个问题以供参考。另见:How to Ask.
        【解决方案8】:

        打印列表中最大数字的索引。

        numbers = [1,2,3,4,5,6,9]
        
        N = 0
        for num in range(len(numbers)) :
          if numbers[num] > N :
            N = numbers[num]
        print(numbers.index(N))
        

        【讨论】:

          【解决方案9】:
          student_scores[1,2,3,4,5,6,7,8,9]
          
          max=student_scores[0]
          for n in range(0,len(student_scores)):
            if student_scores[n]>=max:
              max=student_scores[n]
          print(max)
          # using for loop to go through all items in the list and assign the biggest value to a variable, which was defined as max.
          
          min=student_scores[0]
          for n in range(0,len(student_scores)):
            if student_scores[n]<=min:
              min=student_scores[n]
          print(min)
          # using for loop to go through all items in the list and assign the smallest value to a variable, which was defined as min.
          

          注意:上面的代码是用for循环来获取最大值和最小值的,在其他编程语言中也可以通用。但是,max() 和 min() 函数是在 Python 中使用以获得相同结果的最简单方法。

          【讨论】:

          • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请编辑您的答案以添加解释并说明适用的限制和假设。
          【解决方案10】:

          我也会将此添加为参考。您可以使用排序,然后打印最后一个数字。

          nums = [14, 8, 9, 16, 3, 11, 5]
          
          nums.sort()
          print("Highest number is: ", nums[-1])
          

          【讨论】:

            【解决方案11】:
            scores = [12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27,
                      28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 31, 31, 37,
                      56, 75, 23, 565]
            
            # initialize highest to zero
            highest = 0
            
            for mark in scores:
                if highest < mark:
                    highest = mark
            
            print(mark)
            

            【讨论】:

            • 如果每个值都是负数,则最高将为零。那么答案就错了
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-03-06
            • 1970-01-01
            • 1970-01-01
            • 2017-02-28
            • 2018-05-31
            • 1970-01-01
            • 2021-01-18
            相关资源
            最近更新 更多