Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104577
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Simple exercise |
| Date | Thu, 10 Mar 2016 18:14:11 -0800 |
| Lines | 54 |
| Message-ID | <mailman.169.1457662473.15725.python-list@python.org> (permalink) |
| References | <mailman.117.1457600573.15725.python-list@python.org> <nbt1vd$k00$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de GVS6ApjdTXUSluJEkSrhmgBl7CqoIn1hu7bsRVkiEHoQ== |
| Return-Path | <ckaynor@zindagigames.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'data:': 0.07; 'except:': 0.07; 'cc:addr:python-list': 0.09; 'input,': 0.09; 'def': 0.13; 'thu,': 0.15; '2016': 0.16; '5",': 0.16; 'also:': 0.16; 'key/value': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string': 0.17; 'element': 0.18; 'try:': 0.18; 'version.': 0.18; 'skip:= 10': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'sets': 0.23; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; '"': 0.29; 'print': 0.30; 'entry': 0.31; 'url:python': 0.33; 'subject:Simple': 0.33; 'file': 0.34; 'except': 0.34; 'gives': 0.35; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'serve': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'names': 0.38; 'data': 0.39; 'easily': 0.39; 'rather': 0.39; 'url:3': 0.60; 'skip:n 10': 0.62; 'mar': 0.65; '\xc2\xa0\xc2\xa0': 0.66; 'results.': 0.67; 'skip:\xc2 10': 0.67 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=zindagigames-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=1bOmQwcHYVU6yYCNlB3QprPQbnESnBSPZdQLWEJ7sog=; b=z5I/Gyq2XTTAb6c8P9OepqUKlR3rBh288p80ptW0tr7kQkK5AIamlplfl/WgZBOrgC tvoJRr832Ejxl3SGdgNmXSIETfYxsYdUvKxF/oD3wG0UJmCcBLbSv/Xu+1wIC8o3DAWu cNReiBA3ZS5Y5wPhcpmFACDs65ieXI3+MoYtcol+fNYvY751vJdy2yOQn/NHOPbM9JL2 mjnFI7kCYVcvX46Z9US6QodSANywLIwboawTdvX6c3FiuKcSG706Ez802DOqeSLhkC4H BRI/K+YvoOlGxyMkwsWLXeYbUT1yHuazQz/9kYTa6Zi/l5OvZA8ExIf7FFAU5Lrf/0Rk qc6w== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=1bOmQwcHYVU6yYCNlB3QprPQbnESnBSPZdQLWEJ7sog=; b=dVXwVo06g+d1ycJh18/ScpL9Oh+RX7GaAW7ywktSBJimCW5EBQL//lrhNPcb1zx7e6 XoSlWBOLildcR1sBXG+/xRbEgW6B+DGchYB0Oxz2paEuPNALoyMtmazQhaTBD2yn6zSA vFYxBvtNHoG2IgupN9vc/vY/pyI/d93gE5GPVM3YvCdYN1fYoAO5t2CH5CwxjWNLgIHf yuzI4ZtYThjI8DJ0cpQ6gPwRJQQw1k8ftYdQ7d276dA9a4l0gcNoYStcls0VAcdD/kmG ieEgERxO6WHL4ogEpkqIZSwtQOtuZVvPZTCo4Es9TbGhvCRyF9bU/065lOxkZ5hQmAtw S0XA== |
| X-Gm-Message-State | AD7BkJIAW5WYgwD9QBHdkhrgEvEmjwPGIIX7YLLLZCZbal4IQt+TYXqxdk6sHKz76TlE8P+oK59YrH0mSyqppA== |
| X-Received | by 10.31.192.78 with SMTP id q75mr6922242vkf.96.1457662471111; Thu, 10 Mar 2016 18:14:31 -0800 (PST) |
| In-Reply-To | <nbt1vd$k00$1@dont-email.me> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21 |
| 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:104577 |
Show key headers only | View raw
On Thu, Mar 10, 2016 at 4:05 PM, BartC <bc@freeuk.com> wrote:
> Here's a rather un-Pythonic and clunky version. But it gives the expected
> results. (I've dispensed with file input, but that can easily be added
> back.)
>
> def last(a):
> return a[-1]
>
> def init(a): # all except last element
> return a[0:len(a)-1]
>
> data =["BANANA FRIES 12", # 1+ items/line, last must be numeric
> "POTATO CHIPS 30",
> "APPLE JUICE 10",
> "CANDY 5",
> "APPLE JUICE 10",
> "CANDY 5",
> "CANDY 5",
> "CANDY 5",
> "POTATO CHIPS 30"]
>
> names = [] # serve as key/value sets
> totals = []
>
> for line in data: # banana fries 12
> parts = line.split(" ") # ['banana','fries','12']
> value = int(last(parts)) # 12
> name = " ".join(init(parts)) # 'banana fries'
>
This could be written as (untested):
name, value = line.rsplit(' ', 1) # line.rsplit(maxsplit=1) should also work
value = int(value)
No need to rejoin the string this way.
See also: https://docs.python.org/3.5/library/stdtypes.html#str.rsplit
> try:
> n = names.index(name) # update existing entry
> totals[n] += value
> except:
> names.append(name) # new entry
> totals.append(value)
>
> for i in range(len(names)):
> print (names[i],totals[i])
>
Chris
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simple exercise Rodrick Brown <rodrick.brown@gmail.com> - 2016-03-10 04:02 -0500
Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 11:30 +0100
Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 12:07 +0100
Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 17:05 +0100
Re: Simple exercise Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 17:08 +0100
Re: Simple exercise Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-11 12:24 +1300
Re: Simple exercise Chris Angelico <rosuav@gmail.com> - 2016-03-11 10:38 +1100
Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 00:05 +0000
Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 01:21 +0000
Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 01:45 +0000
Re: Simple exercise Larry Martell <larry.martell@gmail.com> - 2016-03-10 20:53 -0500
Re: Simple exercise "Martin A. Brown" <martin@linux-ip.net> - 2016-03-10 17:56 -0800
Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 02:03 +0000
Re: Simple exercise BartC <bc@freeuk.com> - 2016-03-11 02:18 +0000
Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 07:35 -0700
Re: Simple exercise Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-14 15:06 +0000
Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 09:00 -0700
Re: Simple exercise Steven D'Aprano <steve@pearwood.info> - 2016-03-15 10:59 +1100
Re: Simple exercise Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-15 07:26 +0200
Re: Simple exercise Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-15 19:39 +1100
Re: Simple exercise Chris Angelico <rosuav@gmail.com> - 2016-03-15 19:53 +1100
Re: Simple exercise Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-15 11:04 +0200
Re: Simple exercise Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-15 11:09 +0000
Re: Simple exercise Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-14 09:16 -0600
Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 09:11 -0700
Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-14 15:23 +0000
Re: Simple exercise Peter Otten <__peter__@web.de> - 2016-03-14 17:00 +0100
Re: Simple exercise Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 02:05 +0000
Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 07:07 -0700
Re: Simple exercise Larry Martell <larry.martell@gmail.com> - 2016-03-14 10:13 -0400
Re: Simple exercise alister <alister.ware@ntlworld.com> - 2016-03-14 14:18 +0000
Re: Simple exercise Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 08:22 -0700
Re: Simple exercise MRAB <python@mrabarnett.plus.com> - 2016-03-14 15:57 +0000
Re: Simple exercise Chris Kaynor <ckaynor@zindagigames.com> - 2016-03-10 18:14 -0800
Re: Simple exercise boffi <boffi@casa.sua> - 2016-03-17 22:28 +0100
csiph-web