#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Red face A list comprehension problem


    I am using Python 2.3 and the round() function doesn't seem to work properly within a list comprehension statement. Any idea why this happens?
    Code:
    """
    list pi values progressively rounded off using a list comprehension
    this is supposed to give:
    ['3.1', '3.14', '3.142', '3.1416', '3.14159']
    but gives:
    ['3.1000000000000001', '3.1400000000000001', '3.1419999999999999',
      '3.1415999999999999', '3.1415899999999999']
    """
    piList = [repr(round(355/113.0, k)) for k in range(1,6)]
    print piList
    
    # this rounds off correctly, result = 3.14159
    print round(355/113.0, 5)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    The round function works as it should. What confuses you is repr. This example should make it clear:
    Code:
    >>> repr(3.14)
    '3.14000000000000001'
    Thats how a float is "represented". What gives the result you want is str() (I guess ):
    Code:
    piList = [str(round(355/113.0, k)) for k in range(1,6)]
    Hope this helps.

    Regards, mawe

    Comments on this post

    • netytan agrees
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Unhappy


    Sorry, I am new to this!

    When I use str() I get ...
    TypeError: 'str' object is not callable
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Red face


    Couldn't get str() to work so I removed repr() and now have:
    Code:
    """
    list pi values progressively rounded off using a list comprehension
    this is supposed to give:
    ['3.1', '3.14', '3.142', '3.1416', '3.14159']
    but gives:
    [3.1000000000000001, 3.1400000000000001, 3.1419999999999999,
     3.1415999999999999, 3.1415899999999999]
    """
    piList = [round(355/113.0, k) for k in range(1,6)]
    print piList
    
    # this rounds off correctly, result = 3.14159
    print round(355/113.0, 5)
    Notice that it still gives a goofy result. Any help would be nice!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by Dietrich
    Couldn't get str() to work so I removed repr() and now have:
    Code:
    """
    list pi values progressively rounded off using a list comprehension
    this is supposed to give:
    ['3.1', '3.14', '3.142', '3.1416', '3.14159']
    but gives:
    [3.1000000000000001, 3.1400000000000001, 3.1419999999999999,
     3.1415999999999999, 3.1415899999999999]
    """
    piList = [round(355/113.0, k) for k in range(1,6)]
    print piList
    
    # this rounds off correctly, result = 3.14159
    print round(355/113.0, 5)
    Notice that it still gives a goofy result. Any help would be nice!
    print myobj is equivalent to print str(myobj)
    print mylist is equivalent to print str(mylist) which is equivalent to print str([repr(elem) for elem in mylist])

    Code:
    class Foo(object):
        def __str__(self):
            return "STR"
        def __repr__(self):
            return "REPR"
    
    print Foo()
    print [Foo()]
    As mawe has said, you need to use:
    Code:
    piList = [str(round(355/113.0, k)) for k in range(1,6)]
    --OH.
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Talking


    I finally caught the troublemaker!

    I had looked at a series of list comprehensions and in the previous example had used str as a variable name, so the Python shell did not allow me to use str().

    Works fine now, thanks experts!

IMN logo majestic logo threadwatch logo seochat tools logo