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


Groups > comp.lang.python > #84110

Re: Concerning Dictionaries and += in Python 2.x

From Peter Otten <__peter__@web.de>
Subject Re: Concerning Dictionaries and += in Python 2.x
Date 2015-01-21 09:43 +0100
Organization None
References <bc9e302a-356c-4223-9c25-6a8003db48e6@googlegroups.com> <m9nbrj$i7h$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.17908.1421829827.18130.python-list@python.org> (permalink)

Show all headers | View raw


Denis McMahon wrote:

> On Mon, 19 Jan 2015 16:12:57 -0800, Luke Tomaneng wrote:
> 
>> I have been having a bit of trouble with the things mentioned in the
>> title.
> 
> I've uploaded a slightly different approach to your code at:
> 
> http://www.sined.co.uk/tmp/shop.py.txt
> 
> def compute_bill(shopping):
>     """
>     Takes a dictionary of purchases requested in the form {item: quantity}
>     Returns a tuple of:
>         a dictionary of items supplied in the form {item: quantity}; and
>         the cost of the supplied items
>     """
>     # the invoice amount
>     invoice = 0
>     # what we were able to supply
>     sold = {k:0 for k in shopping.keys()}

There is also dict.from_keys()

>     # check each requested item
>     for item in shopping:
> 
>         # try and sell the requested qty
>         for i in range(shopping[item]):

The inner loop is not just inefficient for stock sold in large quantities, 
it will fail for stock sold by weight, volume etc.

>             # if we have stock remaining
>             if stock[item] > 0:
> 
>                 # reduce stock count for item by 1
>                 stock[item] = stock[item] - 1
>                 # add 1 item to the sale
>                 sold[item] += 1
>                 # add item cost to the invoice
>                 invoice += prices[item]
> 
>     # return the items supplied and their cost
>     return sold, invoice

Here is a possible alternative:

    sold = {}

    for item, wanted_quantity in shopping.iteritems(): # items() in Python 3
        available_quantity = stock.get(item, 0)
        sold_quantity = min(wanted_quantity, available_quantity)
        sold[item] = sold_quantity
        stock[item] -= sold_quantity
        invoice += sold_quantity * prices[item]

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


Thread

Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:12 -0800
  Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:21 +1100
    Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:31 -0800
  Re: Concerning Dictionaries and += in Python 2.x MRAB <python@mrabarnett.plus.com> - 2015-01-20 00:27 +0000
  Re: Concerning Dictionaries and += in Python 2.x Luke Tomaneng <luketomaneng@gmail.com> - 2015-01-19 16:34 -0800
    Re: Concerning Dictionaries and += in Python 2.x Chris Angelico <rosuav@gmail.com> - 2015-01-20 11:46 +1100
  Re: Concerning Dictionaries and += in Python 2.x Dan Stromberg <drsalists@gmail.com> - 2015-01-19 16:54 -0800
  Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 05:01 +0000
    Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:43 +0100
      Re: Concerning Dictionaries and += in Python 2.x Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 14:10 +0000
    Re: Concerning Dictionaries and += in Python 2.x Peter Otten <__peter__@web.de> - 2015-01-21 09:59 +0100

csiph-web