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

        <legend id='64QwQ'><style id='64QwQ'><dir id='64QwQ'><q id='64QwQ'></q></dir></style></legend>

        <small id='64QwQ'></small><noframes id='64QwQ'>

          <bdo id='64QwQ'></bdo><ul id='64QwQ'></ul>
      1. <tfoot id='64QwQ'></tfoot>
      2. 为什么我在这样的文件中找不到最大数量?

        Why can#39;t I find max number in a file like this?(为什么我在这样的文件中找不到最大数量?)

            <bdo id='OrzWh'></bdo><ul id='OrzWh'></ul>
            • <small id='OrzWh'></small><noframes id='OrzWh'>

                <tbody id='OrzWh'></tbody>
                  <tfoot id='OrzWh'></tfoot>
                1. <legend id='OrzWh'><style id='OrzWh'><dir id='OrzWh'><q id='OrzWh'></q></dir></style></legend>
                2. <i id='OrzWh'><tr id='OrzWh'><dt id='OrzWh'><q id='OrzWh'><span id='OrzWh'><b id='OrzWh'><form id='OrzWh'><ins id='OrzWh'></ins><ul id='OrzWh'></ul><sub id='OrzWh'></sub></form><legend id='OrzWh'></legend><bdo id='OrzWh'><pre id='OrzWh'><center id='OrzWh'></center></pre></bdo></b><th id='OrzWh'></th></span></q></dt></tr></i><div id='OrzWh'><tfoot id='OrzWh'></tfoot><dl id='OrzWh'><fieldset id='OrzWh'></fieldset></dl></div>
                  本文介绍了为什么我在这样的文件中找不到最大数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  虽然我对 bash 有很多经验,但我对 python 还是很陌生.我有一个由单列数字组成的文件,我想在列表中找到最大的数字.我尝试使用以下代码:

                  I'm quite new to python, though I have a lot of experience with bash. I have a file that consists of a single column of numbers, and I would like to find the largest number in the list. I tried to do so with the following code:

                  i = 0
                  with open('jan14.nSets.txt','r') as data:
                      for num in data:
                          if num > i:
                              i = num
                  print(i)
                  

                  其中 jan14.nSets.txt 如下:

                  where jan14.nSets.txt is the following:

                  12
                  80
                  46
                  51
                  0
                  64
                  37
                  9
                  270
                  23
                  132
                  133
                  16
                  6
                  18
                  23
                  32
                  75
                  2
                  9
                  6
                  74
                  44
                  41
                  56
                  17
                  9
                  4
                  8
                  5
                  3
                  27
                  1
                  3
                  42
                  23
                  58
                  118
                  100
                  185
                  85
                  63
                  220
                  38
                  163
                  27
                  198
                  

                  我收到的不是 270,而是 9 作为输出,我不明白为什么会这样.我知道有内置插件,但我想知道为什么这不能帮助我理解语言.我正在使用python2.7

                  Rather than 270, I receive 9 as an output, and I do not understand why this is the case. I know that there are builtins for this, but I would like to know why this doesn't work to help me understand the language. I am using python2.7

                  推荐答案

                  num 是一个字符串,而不是一个数字.首先使用 int():

                  num is a string, not a number. Turn it into an integer first using int():

                  num = int(num)
                  

                  您正在比较文本,所以它是按按字典顺序排列的'9' >'80' 因为 ASCII 字符 '9' 的代码点比 '8' 高:

                  You are comparing text, so it is ordered lexicographically, '9' > '80' because the ASCII character '9' has a higher codepoint than '8':

                  >>> '9' > '80'
                  True
                  

                  '9' 行之后,所有其他行的初始数字要么小于 '9',要么该行相等.

                  After the '9' line, all other lines either have an initial digit that is smaller than '9', or the line is equal.

                  您可以使用 max() 函数 代替,前提是您首先使用 map() 将所有行转换为整数:

                  You could use the max() function instead, provided you first use map() to turn all lines into integers:

                  with open('jan14.nSets.txt','r') as data:
                      i = max(map(int, data))
                  

                  或使用 生成器表达式(在 Python 2 中,比 map()) 更节省内存:

                  or use a generator expression (in Python 2, more memory efficient than map()):

                  with open('jan14.nSets.txt','r') as data:
                      i = max(int(num) for num in data)
                  

                  两者都从 data 中提取所有行,将这些行映射为整数,并从中确定最大值.

                  Both pull in all lines from data, map the lines to integers, and determine the maximum value from those.

                  这篇关于为什么我在这样的文件中找不到最大数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

                        <legend id='p2Mi8'><style id='p2Mi8'><dir id='p2Mi8'><q id='p2Mi8'></q></dir></style></legend>

                        • <bdo id='p2Mi8'></bdo><ul id='p2Mi8'></ul>

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