Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104570
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Simple exercise |
| Date | 2016-03-11 01:21 +0000 |
| Message-ID | <mailman.163.1457659326.15725.python-list@python.org> (permalink) |
| References | <mailman.117.1457600573.15725.python-list@python.org> <nbt1vd$k00$1@dont-email.me> |
On 11/03/2016 00:05, BartC wrote:
> On 10/03/2016 09:02, 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
>
>
> 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]
What is wrong with a[0: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'
>
> try:
> n = names.index(name) # update existing entry
> totals[n] += value
> except:
Never use a bare except. Better still, use an appropriate collection
rather than two lists. Off of the top of my head a counter or a
defaultdict.
> names.append(name) # new entry
> totals.append(value)
>
> for i in range(len(names)):
> print (names[i],totals[i])
>
Always a code smell when range() and len() are combined.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
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