Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #55366
| References | <0aa3c507-a570-4d39-823d-106ba63b0b70@googlegroups.com> |
|---|---|
| Date | 2013-10-02 13:19 -0400 |
| Subject | Re: Rounding off Values of dicts (in a list) to 2 decimal points |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.643.1380734819.18130.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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