Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55427

Re: Rounding off Values of dicts (in a list) to 2 decimal points

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'preferably': 0.05; 'reason,': 0.07; 'wednesday,': 0.07; 'check,': 0.09; 'params': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '"b":': 0.16; 'dict': 0.16; 'fine.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; '{"a":': 0.16; "{'a':": 0.16; 'do,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'have:': 0.19; 'code,': 0.22; 'input': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'script.': 0.24; 'string,': 0.24; 'this:': 0.26; 'required.': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'points': 0.29; 'subject:) ': 0.29; 'originally': 0.30; 'subject:list': 0.30; 'gives': 0.31; 'code': 0.31; 'getting': 0.31; 'decimal': 0.31; 'values.': 0.31; 'class': 0.32; 'actual': 0.34; 'skip:_ 10': 0.34; 'subject: (': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'shows': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'list,': 0.38; 'skip:_ 30': 0.39; 'skip:8 10': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'first': 0.61; 'email addr:gmail.com': 0.63; 'show': 0.63; 'places': 0.64; 'due': 0.66; 'goal': 0.75; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Rounding off Values of dicts (in a list) to 2 decimal points
Date Thu, 03 Oct 2013 19:41:36 +0200
Organization None
References <0aa3c507-a570-4d39-823d-106ba63b0b70@googlegroups.com> <23564349-7dd1-4d7a-9bfb-8483ddf3335e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b123.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.686.1380822013.18130.python-list@python.org> (permalink)
Lines 97
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380822013 news.xs4all.nl 15992 [2001:888:2000:d::a6]:37891
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55427

Show key headers only | View raw


tripsvt@gmail.com wrote:

> On Wednesday, October 2, 2013 10:01:16 AM UTC-7, tri...@gmail.com wrote:
>> am trying to round off values in a dict to 2 decimal points but have been
>> unsuccessful so far. The input I have is like this:
>> 
>> 
>> 
>> 
>> 
>>     y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a':
>>     80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b':
>>     0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0,
>>     'c': 10.0, 'd': 10.9762304}]
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> I want to round off all the values to two decimal points using the ceil
>> function. Here's what I have:
>> 
>> 
>> 
>> 
>> 
>>     def roundingVals_toTwoDeci():
>> 
>>         global y
>> 
>>         for d in y:
>> 
>>             for k, v in d.items():
>> 
>>                 v = ceil(v*100)/100.0
>> 
>>         return
>> 
>>     roundingVals_toTwoDeci()
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> But it is not working - I am still getting the old values.
> ____________________________________
> 
> I am not sure what's going on but here's the current scenario: I get the
> values with 2 decimal places as I originally required. When I do
> json.dumps(), it works fine. The goal is to send them to a URL and so I do
> a urlencode. When I decode the urlencoded string, it gives me the same
> goodold 2 decimal places. But, for some reason, at the URL, when I check,
> it no longer limits the values to 2 decimal places, but shows values like
> 9.10003677694312. What's going on. Here's the code that I have:
> 
> class LessPrecise(float):
>     def __repr__(self):
>         return str(self)
> 
> def roundingVals_toTwoDeci(y):
>     for d in y:
>         for k, v in d.iteritems():
>             d[k] = LessPrecise(round(v, 2))
>         return

That should only process the first dict in the list, due to a misplaced 
return.
             
> roundingVals_toTwoDeci(y)
> j = json.dumps(y)
> print j
> 
> //At this point, print j gives me
> 
> [{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c":
> [{0.0, "d": 0.0}, {"a":
> 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0,
> "d": 10.0}]
> 
> //then I do,
> params = urllib.urlencode({'thekey': j})
> 
> //I then decode params and print it and it gives me
> 
> thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b":
> 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0},
> {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
> 
> However, at the URL, the values show up as 90.000043278694123

Can you give the actual code, including the decoding part? Preferably you'd 
put both encoding and decoding into one small self-contained demo script.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-02 10:01 -0700
  Re: Rounding off Values of dicts (in a list) to 2 decimal points Skip Montanaro <skip@pobox.com> - 2013-10-02 12:13 -0500
  Re: Rounding off Values of dicts (in a list) to 2 decimal points Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-02 20:22 +0300
  Re: Rounding off Values of dicts (in a list) to 2 decimal points Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-02 13:19 -0400
  Re: Rounding off Values of dicts (in a list) to 2 decimal points Neil Cerutti <neilc@norwich.edu> - 2013-10-02 17:32 +0000
  Re: Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-03 10:06 -0700
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Peter Otten <__peter__@web.de> - 2013-10-03 19:41 +0200
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Neil Cerutti <neilc@norwich.edu> - 2013-10-03 18:03 +0000
      Re: Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-03 11:17 -0700

csiph-web