Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104497
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Simple exercise |
| Date | Thu, 10 Mar 2016 10:41:34 +0100 |
| Organization | None |
| Lines | 61 |
| Message-ID | <mailman.122.1457602909.15725.python-list@python.org> (permalink) |
| References | <CABRP1o-c+Y7U9owb7_g9WU71FjNo6GT7XVhcuQqgasMrsv2GtA@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de KTH04b9BsbXjluZ81TWtHwmcLBl85UfwiqUgfnrosHww== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; 'sys': 0.05; '__name__': 0.07; 'collections': 0.09; 'here?': 0.09; 'incorrect': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'output': 0.13; "'__main__':": 0.16; 'ordereddict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'input': 0.18; 'seems': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:# 10': 0.27; 'cat': 0.29; "i'm": 0.30; 'code': 0.30; 'skip:. 10': 0.32; 'closely': 0.33; 'subject:Simple': 0.33; 'add': 0.34; 'returning': 0.35; 'skip:i 20': 0.36; 'apple': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'providing': 0.62 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd8240.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| 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> |
| Xref | csiph.com comp.lang.python:104497 |
Show key headers only | View raw
Rodrick Brown wrote: > From the following input > > 9 > BANANA FRIES 12 > POTATO CHIPS 30 > APPLE JUICE 10 > CANDY 5 > APPLE JUICE 10 > CANDY 5 > CANDY 5 > CANDY 5 > POTATO CHIPS 30 > > I'm expecting the following output > BANANA FRIES 12 > POTATO CHIPS 60 > APPLE JUICE 20 > CANDY 20 > > However my code seems be returning incorrect value > > #!/usr/bin/env python3 > > import sys > import re > from collections import OrderedDict > > if __name__ == '__main__': > > od = OrderedDict() > recs = int(input()) > > for _ in range(recs): > file_input = sys.stdin.readline().strip() > m = re.search(r"(\w.+)\s+(\d+)", file_input) > > if m: > if m.group(1) not in od.keys(): > od[m.group(1)] = int(m.group(2)) > else: > od[m.group(1)] += int(od.get(m.group(1),0)) Look closely at the line above. What value do you want to add to the current sum? What value are you actually providing on the right side? > for k,v in od.items(): > print(k,v) > > What's really going on here? > > $ cat groceries.txt | ./groceries.py > BANANA FRIES 12 > POTATO CHIPS 60 > APPLE JUICE 20 > CANDY 40
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Simple exercise Peter Otten <__peter__@web.de> - 2016-03-10 10:41 +0100
csiph-web