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


Groups > comp.lang.python > #45507

Re: Two Dictionaries and a Sum!

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit4.readnews.com!panix!roy
From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Two Dictionaries and a Sum!
Date Sat, 18 May 2013 09:22:47 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Lines 71
Message-ID <roy-9D8691.09224718052013@news.panix.com> (permalink)
References <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com>
NNTP-Posting-Host localhost
X-Trace reader1.panix.com 1368883367 20244 127.0.0.1 (18 May 2013 13:22:47 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Sat, 18 May 2013 13:22:47 +0000 (UTC)
User-Agent MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Xref csiph.com comp.lang.python:45507

Show key headers only | View raw


In article <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com>,
 Bradley Wright <bradley.wright.biz@gmail.com> wrote:

> Confusing subject for a confusing problem (to a novice like me of course!)
> Thx for the help in advance folks
> 
> I have (2) dictionaries:
> 
> prices = {
>     "banana": 4,
>     "apple": 2,
>     "orange": 1.5,
>     "pear": 3
> }
>     
> stock = {
>     "banana": 6,
>     "apple": 0,
>     "orange": 32,
>     "pear": 15
> }
> 
> Here's my instructions:

Hmmm, homework for a class?

> consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!)

I suspect what you're trying to say is that bananas cost BTC 4 each, and 
since you've got 6 bananas, you've got BTC 24 worth of bananas, yes?  
And now you want to find the total value of your fruit supply?

>> HERES MY CODE:
> 
> for key in prices:
>     print prices[key]*stock[key]
> 
> HERES THE OUTPUT:
> 
> 48.0
> 45
> 24
> 0

So far, so good.  A couple of things you may have noticed along the way:

1) Your orange unit price was a float, so the total value of all your 
oranges is a float as well.  That's how math works in Python.

2) The keys are presented in random order.  To make the output easier to 
interpret, you might want to do:

    print key, prices[key]*stock[key]


> ISSUE:
> I need to find a way to add all of those together...any pointers?

The most straight-forward way would be something like:

total = 0
for key in prices:
    fruit_subtotal = prices[key]*stock[key]
    total += fruit_subtotal
    print key, fruit_subtotal

print total


There are better ways to do this in Python, but start like this and get 
that to work.

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


Thread

Two Dictionaries and a Sum! Bradley Wright <bradley.wright.biz@gmail.com> - 2013-05-17 21:19 -0700
  Re: Two Dictionaries and a Sum! Spacelee <fjctlzy@gmail.com> - 2013-05-18 12:30 +0800
  Re: Two Dictionaries and a Sum! nanobio <hektorlg@gmail.com> - 2013-05-17 22:20 -0700
  Re: Two Dictionaries and a Sum! nanobio <hektorlg@gmail.com> - 2013-05-17 22:22 -0700
  Re: Two Dictionaries and a Sum! Chris Angelico <rosuav@gmail.com> - 2013-05-18 15:10 +1000
  Re: Two Dictionaries and a Sum! Roy Smith <roy@panix.com> - 2013-05-18 09:22 -0400

csiph-web