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


Groups > comp.lang.python > #55366

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'parameter': 0.09; 'snippet': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'wrote': 0.14; 'dict': 0.16; 'globals': 0.16; 'loop.': 0.16; 'rewrites': 0.16; "{'a':": 0.16; 'fix': 0.17; 'wrote:': 0.18; 'discussion': 0.18; 'wed,': 0.18; 'trying': 0.19; 'have:': 0.19; 'input': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'this:': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'points': 0.29; 'subject:) ': 0.29; 'said,': 0.30; 'statement': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'getting': 0.31; 'that.': 0.31; 'decimal': 0.31; 'values.': 0.31; 'another': 0.32; 'url:python': 0.33; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'shows': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'pm,': 0.38; 'does': 0.39; 'bad': 0.39; 'skip:8 10': 0.39; 'url:mail': 0.40; 'results.': 0.60; 'you.': 0.62; 'back': 0.62; 'complete': 0.62; 'to:addr:gmail.com': 0.65; 'results': 0.69; 'start.': 0.84; 'joel': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=UB3Yp0Z9NW84m3U0Q1lq1mSOGNl590paWUYvyAGVJbQ=; b=CjwIng1y4xIpeMiO7xpheF8QcLlj2k7oi4d1CTeYEuQdfWjBsl5jArQbyT+JNGjSVg 6Au5oE1B72zP1c5LJtp2+fqLuhUX8SNCmU7GcZKvrX+i5ZRGFUyphGiegeDhfnvSpunR N3tiZQTXE7gE/d9hBWWN108TKK2tDkXdPYpBm0CjWnifVWvFy9g8DuZzXwcJJRwpnUUD V3RP/5KxDfrlc/BS4AhWjy4t+3yJm7hRBLP5LZZ3udgHe1rb8W03Ios/W77lC047YacC hbJnq+puol95aoxwfjQfE8P7izCzhxNH2l3peDN5S6/gjJK0GN54JWzDW7xOtnHPFX1w yyEQ==
MIME-Version 1.0
X-Received by 10.52.32.37 with SMTP id f5mr2450561vdi.17.1380734395057; Wed, 02 Oct 2013 10:19:55 -0700 (PDT)
In-Reply-To <0aa3c507-a570-4d39-823d-106ba63b0b70@googlegroups.com>
References <0aa3c507-a570-4d39-823d-106ba63b0b70@googlegroups.com>
Date Wed, 2 Oct 2013 13:19:54 -0400
Subject Re: Rounding off Values of dicts (in a list) to 2 decimal points
From Joel Goldstick <joel.goldstick@gmail.com>
To tripsvt@gmail.com
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
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.643.1380734819.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380734819 news.xs4all.nl 15963 [2001:888:2000:d::a6]:44708
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55366

Show key headers only | View raw


On Wed, Oct 2, 2013 at 1:01 PM,  <tripsvt@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:

This is a snippet of what you have I am guessing.  There is no print
statement so you won't be able to see the results.  Its best if you
include your complete code (if its short) or an example that actually
shows the problem..
>
>
>     def roundingVals_toTwoDeci():
>         global y
>         for d in y:
>             for k, v in d.items():
>                 v = ceil(v*100)/100.0
>         return
>     roundingVals_toTwoDeci()
>
>
That being said, you should pass y as a parameter to your function.
Using globals is always a bad idea.  That's another discussion
entirely, but you should google why globals are a bad idea to learn
more.
Your code does a calculation to create a value you call v.  You should
put a print statement below that to see what v has become.  Your inner
loop rewrites v for each loop.  It actually re-writes it twice i think
-- once when it iterates, and once when it calculates.  So you need to
fix that.  Also I think you need to interate using d.interitems()

That's a start.  Come back with the code you actually wrote and the
results it showed you.
Af


>
> But it is not working - I am still getting the old values.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

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