about max() and min()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leoxu1982
    New Member
    • Sep 2011
    • 6

    about max() and min()

    I met a situation like this:
    >>> li=['926.7', '930.6', '931.7', '933', '934.1', '936.5', '937.5', '940.5', '941.6', '944.6', '945.7', '947.2', '948.6', '950.4', '953.4', '957', '958.6', '962.1', '966.5', '968.2', '969.7', '970.4', '973.1', '974.6', '975.3', '983.9', '989.8', '996.5', '999.3', '1001.8', '1006.1', '1010.5', '1011.9', '1012.6', '1013.9', '1023.8', '1034.3', '1035.3', '1035.9', '1038.4', '1044.3', '1045.8', '1046.8', '1055.2', '1057.5', '1063.2', '1065', '1069.5', '1070.8', '1080', '1085', '1087.1', '1101.1', '1102', '1102.7', '1115.7', '1125.1', '1136.6']
    >>> li
    ['926.7', '930.6', '931.7', '933', '934.1', '936.5', '937.5', '940.5', '941.6', '944.6', '945.7', '947.2', '948.6', '950.4', '953.4', '957', '958.6', '962.1', '966.5', '968.2', '969.7', '970.4', '973.1', '974.6', '975.3', '983.9', '989.8', '996.5', '999.3', '1001.8', '1006.1', '1010.5', '1011.9', '1012.6', '1013.9', '1023.8', '1034.3', '1035.3', '1035.9', '1038.4', '1044.3', '1045.8', '1046.8', '1055.2', '1057.5', '1063.2', '1065', '1069.5', '1070.8', '1080', '1085', '1087.1', '1101.1', '1102', '1102.7', '1115.7', '1125.1', '1136.6']
    >>> max(li)
    '999.3'
    >>> min(li)
    '1001.8'
    >>>

    I don't know why.
    Can anyone answer me?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    A string evaluates different from a float or integer.
    Code:
    >>> "1000" > "999"
    False
    >>> 1000 > 999
    True
    >>>

    Comment

    • leoxu1982
      New Member
      • Sep 2011
      • 6

      #3
      bvdet
      thank u!
      nevertheless, how do I deal with situation like this, I store string of number in List

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Your list is already sorted least value to largest value. li[0] is the minimum and li[-1] is the maximum.

        Comment

        • leoxu1982
          New Member
          • Sep 2011
          • 6

          #5
          Yes,this situation is special,but more are disorder, how to deal with those?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Store them as numbers instead of strings.

            Comment

            • leoxu1982
              New Member
              • Sep 2011
              • 6

              #7
              like this
              [ '132.365', '132.148', '131.410', '131.237', '131.410', '130.586', '129.588', '128.330', '126.725', '124.686', '123.471', '124.122', '126.681', '129.545', '131.063', '131.410', '131.106', '130.499', '130.412', '130.456', '129.631', '127.853', '127.679', '129.588', '133.146', '135.228', '135.879', '135.879', '136.226', '137.788', '140.564', '143.818', '145.640', '146.464', '146.508', '145.423', '142.430', '138.525', '133.276', '128.330', '125.119', '122.820', '121.258', '119.436', '117.788', '116.269', '116.573', '117.614', '119.480', '121.128', '122.039', '121.345', '120.304', '119.349', '119.176', '119.263', '119.176', '118.829', '118.829', '119.393', '120.304', '120.781', '120.694', '120.391', '120.998', '122.126', '123.341', '123.341', '122.560', '122.516', '123.514', '124.425', '125.640', '127.462', '129.762', '131.020', '130.846', '129.154', '126.074', '123.601', '121.736', '120.955', '120.694', '120.868', '121.605', '123.080', '124.599', '125.076', '124.903', '124.599', '125.467', '126.725', '128.503', '130.282', '130.716', '130.022', '129.371', '128.634', '126.551', '124.078', '122.733', '122.386', '122.907', '124.469', '127.072', '128.634', '129.111', '129.284', '129.675', '130.152', '129.198', '127.506', '125.467', '124.642', '125.380', '125.814', '123.861', '118.829', '112.104', '108.156', '105.770', '102.820', '98.829', '96.573', '95.662', '95.011', '94.100', '93.753', '94.143', '94.360', '94.707', '94.317', '95.228', '97.961', '101.085', '103.818', '104.469', '103.471', '102.603', '102.777', '103.905', '104.642', '104.816', '104.555', '104.208', '104.208', '104.555', '105.033', '105.727', '106.681', '107.636', '109.284', '112.408', '115.662', '119.002', '120.868', '121.952', '122.820', '123.341', '124.382', '126.985', '131.323', '134.057', '134.881', '134.230', '132.451', '130.846', '129.111', '126.985', '125.336', '123.688', '121.432', '120.044', '119.176', '118.005', '116.226', '114.230', '110.673', '104.469', '100.608', '99.480', '100.130', '101.215', '101.866', '102.083', '102.126', '102.343', '102.690', '103.080', '103.775', '105.727', '108.634', '111.323', '113.970', '115.358', '115.792', '115.618', '115.792', '115.705', '114.577', '113.709', '113.362', '113.319', '113.059', '112.712', '112.712', '113.579', '114.360', '115.271', '116.703', '118.135', '118.091', '116.356', '114.057', '112.018', '110.673', '110.326', '110.890', '112.104', '113.059', '113.623', '114.013', '114.404', '115.488', '117.441', '119.436', '120.347', '120.434', '120.174', '120.087', '119.827']

              Comment

              • leoxu1982
                New Member
                • Sep 2011
                • 6

                #8
                but these numbers I read from txt, I am newbie

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Don't use any quotes, quotes signify a string.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Your list of strings can be converted to a list of floats with a list comprehension. Or if you prefer to keep them as strings, you can sort the list to determine the max and min.
                    Code:
                    >>> [float(item) for item in s]
                    [800.89999999999998, 910.39999999999998, 999.89999999999998, 1000.4, 2000.0999999999999]
                    >>>
                    Code:
                    >>> s = ["910.4", "1000.4", "800.9", "2000.1", "999.9"]
                    >>> s.sort(lambda a, b: cmp(float(a), float(b)))
                    >>> s
                    ['800.9', '910.4', '999.9', '1000.4', '2000.1']
                    >>>
                    Last edited by bvdet; Sep 16 '11, 03:58 PM.

                    Comment

                    • leoxu1982
                      New Member
                      • Sep 2011
                      • 6

                      #11
                      bvde's first solution maybe suitable , because I need the index of the max value in the list,I can't rearrange it and list[0] is stored a string of the series name.
                      I will try bvde's first solution

                      Comment

                      Working...