How do I round numbers in a list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel Valenzue
    New Member
    • Dec 2010
    • 39

    How do I round numbers in a list?

    I want to round a list of numbers to basically remove all the decimal places or even just convert them to integers.

    So this is what I've tried and I just can't get this to work.

    Code:
    myList = [10.5,25.1,350.6,4,5,6] #declare my list
    for i in range(len(myList)):
        myList[i] == round(i,0)  #tries to round all numbers.
    print myList
    
    #other Attempt
    for i in range(len(myList)):
        myList[i] == int(i)  #tries to make i into an integer
    print myList
    
    #other attempt
    
    for i in myList:
         int(i)
    I'm just not getting this.

    Thanks
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    So what error message are you getting?
    So what output, if any, are you getting?
    Hint: round(i,0) returns a double so if you want to round and then return the integer part only...

    -z

    Comment

    • Miguel Valenzue
      New Member
      • Dec 2010
      • 39

      #3
      Okay, here's the code again:
      Code:
      myList = [10.5,25.1,350.6,4,5,6]
      
      for i in myList:
          int(i)
      
      print myList
      
      for i in myList:
          round(i)
      
      print myList
      
      for i in range(len(myList)):
          myList[i] == round(i,0)
      
      print myList
      Here's my output

      [10.5, 25.1, 350.6, 4, 5, 6]
      [10.5, 25.1, 350.6, 4, 5, 6]
      [10.5, 25.1, 350.6, 4, 5, 6]

      My desirec output is
      [11, 25, 351, 4, 5, 6]

      No errors. Just doesn't do anything.
      When you say it returns a double. clueless on what that implies.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Returns a data type of "double" vs integer, long, etc..
        Data types are covered in most good books and you can google on them as related to python/ruby/vba/.net etc...

        My hint has the solution.

        -z

        Comment

        • Miguel Valenzue
          New Member
          • Dec 2010
          • 39

          #5
          I will call you Gollum from now own, master of riddles. ;)
          Okay, I understand double.
          So if round is returning a double, then I need to convert that into an integer.

          I've tried that (at least I think) with this code.
          Code:
          for i in myList:
              int((round(i)))
          
          print myList
          But on a deeper level, is my logic right? to redefine the numbers (long) in a list by just using the for loop?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            In your first example, you are using the comparison operator "==" when you should be using the assignment operator "=". Also, you should combine int() and round() to achieve the desired output.
            Code:
            >>> int(round(12.5, 0))
            13
            >>> x == int(round(12.5, 0))
            False
            >>> x = int(round(12.5, 0))
            >>> x
            13
            >>>
            A list comprehension is the easiest way.
            Code:
            >>> x = [1,2,3,11.5]
            >>> newlist = [int(round(n, 0)) for n in x]
            >>> newlist
            [1, 2, 3, 12]
            >>>

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              bvdet
              sigh... you let 'em off the hook. :(
              -z

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Sorry about that zmbd. If he was going to "get it" he already would have.

                Comment

                • Miguel Valenzue
                  New Member
                  • Dec 2010
                  • 39

                  #9
                  Well, first of all, thanks both of you. I appreciate your time.
                  But after looking at the way I was approaching it, I want to learn a bit more here.
                  I got the code to work so now my vector multiplication that I was going for is complete.

                  But this was my thought process.

                  If I put
                  Code:
                  int(12.5,0)
                  in the command line of python, it returns 13

                  So that was fine with me. I get that.

                  But this was my thinking.
                  If I have a list, I can do something real basic like this

                  Code:
                  myList =[4,3,5,6,1]
                  
                  for range in (len(myList)):
                       i=*3
                  That returns a new list of
                  12,9,15,6,1

                  So my thought process was, why can't I just use the round or integer function within the same statement such as

                  Code:
                  for range in (len(myList)):
                       i=int(i)
                  But when I do this, it returns a number like 2.000000001 or something like that.

                  Anyhow, that's what I don't get so as part of my learning experience, can you help me understand what is happening? I see that round returns a double like you said, but how does that double go back to being an int?

                  I mean, why doesn´t the following code work?

                  Code:
                  for range in (len(myList)):
                       i=int(round(i,0))
                  thanks again
                  Oh, and bvdet,
                  I didn´t know you could use list comprehension like that.
                  I redid all my code and cut it down by about 25% with that new found knowledge.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    In your for loop, range is used as a temporary identifier for the expression len(myList), which returns an integer. You cannot iterate on an integer. Also, you will mask the built-in function range(). I think you meant to do this:
                    Code:
                    >>> myList = [10.2, 12.9]
                    >>> for i in range(len(myList)):
                    ... 	myList[i]=int(round(myList[i],0))
                    ... 	
                    >>> myList
                    [10, 13]
                    >>>
                    The temporary identifier i can be used as the index for getting each list element and reassigning the element at that index. Identifier i will retain the last value assigned to it. Built-in function range() returns a list.
                    Code:
                    >>> range(len(myList))
                    [0, 1]
                    >>> i
                    1
                    >>>
                    HTH

                    Comment

                    Working...