<i id='zj2xW'><tr id='zj2xW'><dt id='zj2xW'><q id='zj2xW'><span id='zj2xW'><b id='zj2xW'><form id='zj2xW'><ins id='zj2xW'></ins><ul id='zj2xW'></ul><sub id='zj2xW'></sub></form><legend id='zj2xW'></legend><bdo id='zj2xW'><pre id='zj2xW'><center id='zj2xW'></center></pre></bdo></b><th id='zj2xW'></th></span></q></dt></tr></i><div id='zj2xW'><tfoot id='zj2xW'></tfoot><dl id='zj2xW'><fieldset id='zj2xW'></fieldset></dl></div>

    <small id='zj2xW'></small><noframes id='zj2xW'>

  • <tfoot id='zj2xW'></tfoot><legend id='zj2xW'><style id='zj2xW'><dir id='zj2xW'><q id='zj2xW'></q></dir></style></legend>

        <bdo id='zj2xW'></bdo><ul id='zj2xW'></ul>

        从数字列表返回3个最大值的函数出错

        Error in function to return 3 largest values from a list of numbers(从数字列表返回3个最大值的函数出错)

      1. <small id='ueVkb'></small><noframes id='ueVkb'>

          <i id='ueVkb'><tr id='ueVkb'><dt id='ueVkb'><q id='ueVkb'><span id='ueVkb'><b id='ueVkb'><form id='ueVkb'><ins id='ueVkb'></ins><ul id='ueVkb'></ul><sub id='ueVkb'></sub></form><legend id='ueVkb'></legend><bdo id='ueVkb'><pre id='ueVkb'><center id='ueVkb'></center></pre></bdo></b><th id='ueVkb'></th></span></q></dt></tr></i><div id='ueVkb'><tfoot id='ueVkb'></tfoot><dl id='ueVkb'><fieldset id='ueVkb'></fieldset></dl></div>
          <tfoot id='ueVkb'></tfoot>
            <bdo id='ueVkb'></bdo><ul id='ueVkb'></ul>

            • <legend id='ueVkb'><style id='ueVkb'><dir id='ueVkb'><q id='ueVkb'></q></dir></style></legend>

                  <tbody id='ueVkb'></tbody>
                1. 本文介绍了从数字列表返回3个最大值的函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这个数据文件,我必须找到它包含的3个最大数字

                  24.7    25.7    30.6    47.5    62.9    68.5    73.7    67.9    61.1    48.5    39.6    20.0
                  16.1    19.1    24.2    45.4    61.3    66.5    72.1    68.4    60.2    50.9    37.4    31.1
                  10.4    21.6    37.4    44.7    53.2    68.0    73.7    68.2    60.7    50.2    37.2    24.6
                  21.5    14.7    35.0    48.3    54.0    68.2    69.6    65.7    60.8    49.1    33.2    26.0
                  19.1    20.6    40.2    50.0    55.3    67.7    70.7    70.3    60.6    50.7    35.8    20.7
                  14.0    24.1    29.4    46.6    58.6    62.2    72.1    71.7    61.9    47.6    34.2    20.4
                  8.4     19.0    31.4    48.7    61.6    68.1    72.2    70.6    62.5    52.7    36.7    23.8
                  11.2    20.0    29.6    47.7    55.8    73.2    68.0    67.1    64.9    57.1    37.6    27.7
                  13.4    17.2    30.8    43.7    62.3    66.4    70.2    71.6    62.1    46.0    32.7    17.3
                  22.5    25.7    42.3    45.2    55.5    68.9    72.3    72.3    62.5    55.6    38.0    20.4
                  17.6    20.5    34.2    49.2    54.8    63.8    74.0    67.1    57.7    50.8    36.8    25.5
                  20.4    19.6    24.6    41.3    61.8    68.5    72.0    71.1    57.3    52.5    40.6    26.2
                  

                  因此,我编写了以下代码,但它只搜索第一行数字,而不是整个列表。是否有人可以帮助查找错误?

                  def three_highest_temps(f):
                      file = open(f, "r")
                      largest = 0
                      second_largest = 0
                      third_largest = 0
                      temp = []
                      for line in file:
                          temps = line.split()
                  
                          for i in temps:
                              if i > largest:
                                  largest = i
                              elif largest > i > second_largest:
                                  second_largest = i
                              elif second_largest > i > third_largest:
                                  third_largest = i
                          return largest, second_largest, third_largest
                  
                  print(three_highest_temps("data5.txt"))
                  

                  推荐答案

                  您的return语句在for循环中。一旦到达RETURN,函数就会终止,因此循环永远不会进入第二次迭代。通过减少缩进将return移出循环。

                      for line in file:
                          temps = line.split()
                  
                          for i in temps:
                              if i > largest:
                                  largest = i
                              elif largest > i > second_largest:
                                  second_largest = i
                              elif second_largest > i > third_largest:
                                  third_largest = i
                      return largest, second_largest, third_largest
                  

                  此外,您的比较不会起作用,因为line.split()返回字符串列表,而不是浮点数。(如前所述,您的数据由浮点数组成,而不是整数。我假设任务是找到最大的浮存。)因此,让我们使用float()

                  转换字符串

                  您的代码仍然不会正确,因为当您找到一个新的最大值时,您将完全丢弃旧的值。相反,您现在应该将其视为第二大已知值。同样的规则适用于第二到第三大。

                      for line in file:
                          temps = line.split()
                  
                          for temp_string in temps:
                              i = float(temp_string)
                              if i > largest:
                                  third_largest = second_largest
                                  second_largest = largest
                                  largest = i
                              elif largest > i > second_largest:
                                  third_largest = second_largest
                                  second_largest = i
                              elif second_largest > i > third_largest:
                                  third_largest = i
                      return largest, second_largest, third_largest
                  

                  现在还有最后一个问题:

                  您忽略了I与最大值之一相同的情况。在这种情况下,i > largest将是假的,但largest > i也是假的。您可以将这两个比较中的任何一个更改为>=来修复此问题。

                  相反,让我们简化if子句,因为elif条件只有在所有前面的条件都已被发现为假之后才会考虑。当我们到达第一个elif时,我们已经知道i不可能大于largest,所以将它与second largest进行比较就足够了。第二个elif也是如此。

                      for line in file:
                          temps = line.split()
                  
                          for temp_string in temps:
                              i = float(temp_string)
                              if i > largest:
                                  third_largest = second_largest
                                  second_largest = largest
                                  largest = i
                              elif i > second_largest:
                                  third_largest = second_largest
                                  second_largest = i
                              elif i > third_largest:
                                  third_largest = i
                      return largest, second_largest, third_largest
                  

                  这样可以避免意外过滤掉i == largesti == second_largest边缘案例。

                  这篇关于从数字列表返回3个最大值的函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                  • <bdo id='NUTPL'></bdo><ul id='NUTPL'></ul>

                      <tbody id='NUTPL'></tbody>

                    <small id='NUTPL'></small><noframes id='NUTPL'>

                  • <legend id='NUTPL'><style id='NUTPL'><dir id='NUTPL'><q id='NUTPL'></q></dir></style></legend>
                    <tfoot id='NUTPL'></tfoot>

                          • <i id='NUTPL'><tr id='NUTPL'><dt id='NUTPL'><q id='NUTPL'><span id='NUTPL'><b id='NUTPL'><form id='NUTPL'><ins id='NUTPL'></ins><ul id='NUTPL'></ul><sub id='NUTPL'></sub></form><legend id='NUTPL'></legend><bdo id='NUTPL'><pre id='NUTPL'><center id='NUTPL'></center></pre></bdo></b><th id='NUTPL'></th></span></q></dt></tr></i><div id='NUTPL'><tfoot id='NUTPL'></tfoot><dl id='NUTPL'><fieldset id='NUTPL'></fieldset></dl></div>