Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; '"""': 0.07; 'remaining': 0.07; 'item,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'title.': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '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; 'supplied': 0.16; 'tuple': 0.16; 'url:py': 0.16; 'weight,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'slightly': 0.19; 'header:User-Agent:1': 0.23; 'mon,': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'code': 0.31; 'trouble': 0.34; 'at:': 0.34; 'add': 0.35; 'there': 0.35; 'possible': 0.36; 'invoice': 0.37; 'to:addr:python-list': 0.38; 'stock': 0.39; 'volume': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'mentioned': 0.61; 'different': 0.65; 'here': 0.66; 'url:co': 0.67; 'of:': 0.68; 'sale': 0.75; '2015': 0.84; 'inefficient': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Concerning Dictionaries and += in Python 2.x Date: Wed, 21 Jan 2015 09:43:31 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd88a7.dip0.t-ipconnect.de User-Agent: KNode/4.13.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421829827 news.xs4all.nl 2845 [2001:888:2000:d::a6]:41465 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84110 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]