finding the biggest item in a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    finding the biggest item in a list

    lets say i have this list:
    numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

    and i want my script to tell me that the highest number in the list is '20'

    how can i do that?

    thnx
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    lets say i have this list:
    numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

    and i want my script to tell me that the highest number in the list is '20'

    how can i do that?

    thnx
    [code=Python]>>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
    >>> max(numbers)
    '8'
    >>> max([float(n) for n in numbers])
    20.0
    >>> [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by Patrick C
      lets say i have this list:
      numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

      and i want my script to tell me that the highest number in the list is '20'

      how can i do that?

      thnx
      [CODE=python]
      >>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
      >>> floatNums = [float(x) for x in numbers]
      >>> max(floatNums)
      20.0
      >>> # but that won't work on strings #
      >>> max(numbers)
      '8'
      >>> [/CODE]

      Comment

      • Patrick C
        New Member
        • Apr 2007
        • 54

        #4
        what's the difference from what you're doing in what i'm trying below. i'll tell you one difference: what you do works, and what I'm trying doesn't.
        Code:
        >>>import re
        >>> stuff
        '37.04\n36.55\n35.05\n35.25\n34.68\n34.02\n34.77\n34.3\n33.79\n33.31\n32.82\n33.36\n33.52\n35.24\n35.92\n35.79\n37.8\n36.4\n36.89\n36.19\n37.82\n37.77\n37.17\n37.81\n36.75\n37.3\n38.04\n37.95\n38.96\n39.39\n39.7\n40.04\n40.15\n40.58\n40.87\n40.94\n40.48\n40.25\n40.23\n40.3\n39.83\n39.2\n39.37\n39.35\n39.52\n39.76\n39.15\n39.18\n39.36\n39.95\n40.03\n38.27\n37.96\n37.95\n37.79\n37.73\n37.36\n37.71\n37.95\n37.66\n38.3\n38.86'
        >>> stuff2 = re.split('\n', prices)
        >>> stuff2
        ['37.04', '36.55', '35.05', '35.25', '34.68', '34.02', '34.77', '34.3', '33.79', '33.31', '32.82', '33.36', '33.52', '35.24', '35.92', '35.79', '37.8', '36.4', '36.89', '36.19', '37.82', '37.77', '37.17', '37.81', '36.75', '37.3', '38.04', '37.95', '38.96', '39.39', '39.7', '40.04', '40.15', '40.58', '40.87', '40.94', '40.48', '40.25', '40.23', '40.3', '39.83', '39.2', '39.37', '39.35', '39.52', '39.76', '39.15', '39.18', '39.36', '39.95', '40.03', '38.27', '37.96', '37.95', '37.79', '37.73', '37.36', '37.71', '37.95', '37.66', '38.3', '38.86', '']
        >>> floatStuff = [float(i) for i in stuff2]
        Traceback (most recent call last):
          File "<interactive input>", line 1, in <module>
        ValueError: empty string for float()

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by Patrick C
          what's the difference from what you're doing in what i'm trying below. i'll tell you one difference: what you do works, and what I'm trying doesn't.
          Code:
          >>>import re
          >>> stuff
          '37.04\n36.55\n35.05\n35.25\n34.68\n34.02\n34.77\n34.3\n33.79\n33.31\n32.82\n33.36\n33.52\n35.24\n35.92\n35.79\n37.8\n36.4\n36.89\n36.19\n37.82\n37.77\n37.17\n37.81\n36.75\n37.3\n38.04\n37.95\n38.96\n39.39\n39.7\n40.04\n40.15\n40.58\n40.87\n40.94\n40.48\n40.25\n40.23\n40.3\n39.83\n39.2\n39.37\n39.35\n39.52\n39.76\n39.15\n39.18\n39.36\n39.95\n40.03\n38.27\n37.96\n37.95\n37.79\n37.73\n37.36\n37.71\n37.95\n37.66\n38.3\n38.86'
          >>> stuff2 = re.split('\n', prices)
          >>> stuff2
          ['37.04', '36.55', '35.05', '35.25', '34.68', '34.02', '34.77', '34.3', '33.79', '33.31', '32.82', '33.36', '33.52', '35.24', '35.92', '35.79', '37.8', '36.4', '36.89', '36.19', '37.82', '37.77', '37.17', '37.81', '36.75', '37.3', '38.04', '37.95', '38.96', '39.39', '39.7', '40.04', '40.15', '40.58', '40.87', '40.94', '40.48', '40.25', '40.23', '40.3', '39.83', '39.2', '39.37', '39.35', '39.52', '39.76', '39.15', '39.18', '39.36', '39.95', '40.03', '38.27', '37.96', '37.95', '37.79', '37.73', '37.36', '37.71', '37.95', '37.66', '38.3', '38.86', '']
          >>> floatStuff = [float(i) for i in stuff2]
          Traceback (most recent call last):
            File "<interactive input>", line 1, in <module>
          ValueError: empty string for float()
          Line #10 says it all. """ValueErr or: empty string for float()"""
          Since it appears to be the very last entry only, you could do:[CODE=python]
          >>> floatStuff = [float(i) for i in stuff2[:-1]] # "slice" the last element off[/CODE]
          or
          [CODE=python]>>> stuff2 = re.split('\n', prices)[:-1] # "slice" the last element off[/CODE]
          or (but I haven't tested this. If may evaluate the float() before the if)
          [CODE=python]>>> # use the "predicate" part of the list comprehension.
          >>> floatStuff = [float(floatStr) for floatStr in stuff2 if floatStr][/CODE]

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            i don't know why you are using re. however, you should always go for the basics if its available
            Code:
            >>> stuff.strip().split()

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by ghostdog74
              i don't know why you are using re. however, you should always go for the basics if its available
              Code:
              >>> stuff.strip().split()
              A very elegant solution IFF the values in the file can be guaranteed to be valid floats.

              Comment

              Working...