Re: Simple exercise

2016-03-19 Thread boffi
Rodrick Brown writes: > 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 >>> data =["BANANA FRIES 12", ..."P

Re: Simple exercise

2016-03-15 Thread Oscar Benjamin
On 14 March 2016 at 23:59, Steven D'Aprano wrote: > On Tue, 15 Mar 2016 02:06 am, Oscar Benjamin wrote: > >> On 14 March 2016 at 14:35, Rick Johnson >> wrote: >>> >>> I would strongly warn anyone against using the zip function >>> unless >> ... >>> I meant to say: absolutely, one hundred percent

Re: Simple exercise

2016-03-15 Thread Jussi Piitulainen
Steven D'Aprano writes: > On Tuesday 15 March 2016 16:26, Jussi Piitulainen wrote: > >> Steven D'Aprano writes: >> >>> Unfortunate or not, it seems to be quite common that "zip" >>> (convolution) discards items when sequences are of different lengths. >> >> Citation needed. Where is zip called c

Re: Simple exercise

2016-03-15 Thread Chris Angelico
On Tue, Mar 15, 2016 at 7:39 PM, Steven D'Aprano wrote: >> Why should zip be called convolution? > > Why should anything be called anything? > > Don't worry, I'm not suggesting that the zip function be renamed. > It's like referring to the 'and' and 'or' operators as conjunctions and disjunctions

Re: Simple exercise

2016-03-15 Thread Steven D'Aprano
On Tuesday 15 March 2016 16:26, Jussi Piitulainen wrote: > Steven D'Aprano writes: > >> Unfortunate or not, it seems to be quite common that "zip" >> (convolution) discards items when sequences are of different lengths. > > Citation needed. Where is zip called convolution? Wikipedia :-) Unfort

Re: Simple exercise

2016-03-14 Thread Jussi Piitulainen
Steven D'Aprano writes: > Unfortunate or not, it seems to be quite common that "zip" > (convolution) discards items when sequences are of different lengths. Citation needed. Where is zip called convolution? Why should zip be called convolution? > See https://en.wikipedia.org/wiki/Convolution_%2

Re: Simple exercise

2016-03-14 Thread Steven D'Aprano
On Tue, 15 Mar 2016 02:06 am, Oscar Benjamin wrote: > On 14 March 2016 at 14:35, Rick Johnson > wrote: >> >> I would strongly warn anyone against using the zip function >> unless > ... >> I meant to say: absolutely, one hundred percent *SURE*, that >> both sequences are of the same length, or, ab

Re: Simple exercise

2016-03-14 Thread Rick Johnson
On Monday, March 14, 2016 at 10:17:54 AM UTC-5, Ian wrote: > It's sometimes very useful [for zip to discard values], > though. The obvious solution is to allow the caller to decide if the error should be raised, or not. Currently, the caller has no control over the internals of zip unless he crea

Re: Simple exercise

2016-03-14 Thread Rick Johnson
On Monday, March 14, 2016 at 10:06:56 AM UTC-5, Oscar Benjamin wrote: > On 14 March 2016 at 14:35, Rick Johnson wrote: > > > > I would strongly warn anyone against using the zip function > > unless > ... > > I meant to say: absolutely, one hundred percent *SURE*, that > > both sequences are of the

Re: Simple exercise

2016-03-14 Thread Peter Otten
Ian Kelly wrote: > On Mon, Mar 14, 2016 at 9:06 AM, Oscar Benjamin > wrote: >> On 14 March 2016 at 14:35, Rick Johnson >> wrote: >>> >>> I would strongly warn anyone against using the zip function >>> unless >> ... >>> I meant to say: absolutely, one hundred percent *SURE*, that >>> both sequenc

Re: Simple exercise

2016-03-14 Thread MRAB
On 2016-03-14 15:22, Rick Johnson wrote: On Monday, March 14, 2016 at 9:19:04 AM UTC-5, alister wrote: A code smell does not necessarily mean the code is wrong, just that it warrants investigation as there is a strong possibility it may be sub- optimal Yes, technically speaking, you're correct

Re: Simple exercise

2016-03-14 Thread Rick Johnson
On Monday, March 14, 2016 at 9:19:04 AM UTC-5, alister wrote: > A code smell does not necessarily mean the code is wrong, > just that it warrants investigation as there is a strong > possibility it may be sub- optimal Yes, technically speaking, you're correct. But the concept of "code smell" has

Re: Simple exercise

2016-03-14 Thread Mark Lawrence
On 14/03/2016 15:06, Oscar Benjamin wrote: On 14 March 2016 at 14:35, Rick Johnson wrote: I would strongly warn anyone against using the zip function unless ... I meant to say: absolutely, one hundred percent *SURE*, that both sequences are of the same length, or, absolutely one hundred perc

Re: Simple exercise

2016-03-14 Thread Ian Kelly
On Mon, Mar 14, 2016 at 9:06 AM, Oscar Benjamin wrote: > On 14 March 2016 at 14:35, Rick Johnson wrote: >> >> I would strongly warn anyone against using the zip function >> unless > ... >> I meant to say: absolutely, one hundred percent *SURE*, that >> both sequences are of the same length, or, a

Re: Simple exercise

2016-03-14 Thread Oscar Benjamin
On 14 March 2016 at 14:35, Rick Johnson wrote: > > I would strongly warn anyone against using the zip function > unless ... > I meant to say: absolutely, one hundred percent *SURE*, that > both sequences are of the same length, or, absolutely one > hundred percent *SURE*, that dropping values is n

Re: Simple exercise

2016-03-14 Thread Rick Johnson
On Thursday, March 10, 2016 at 8:04:04 PM UTC-6, Mark Lawrence wrote: > On 11/03/2016 01:45, BartC wrote: > > [...] > > Any other way of traversing two lists in parallel? > > > > Use zip() Sure, the zip function is quite handy, but it can produce subtle bugs when both sequences are not of the same

Re: Simple exercise

2016-03-14 Thread alister
On Mon, 14 Mar 2016 07:07:45 -0700, Rick Johnson wrote: > On Thursday, March 10, 2016 at 7:22:26 PM UTC-6, Mark Lawrence wrote: >> Always a code smell when range() and len() are combined. > > I would be careful about dealing in absolutes Mark. a code smell does not necesarily mean the code is wr

Re: Simple exercise

2016-03-14 Thread Larry Martell
On Mon, Mar 14, 2016 at 10:07 AM, Rick Johnson wrote: > On Thursday, March 10, 2016 at 7:22:26 PM UTC-6, Mark Lawrence wrote: >> Always a code smell when range() and len() are combined. > > I would be careful about dealing in absolutes Mark. I always think people should never use absolutes. -- h

Re: Simple exercise

2016-03-14 Thread Rick Johnson
On Thursday, March 10, 2016 at 7:22:26 PM UTC-6, Mark Lawrence wrote: > Always a code smell when range() and len() are combined. I would be careful about dealing in absolutes Mark. -- https://mail.python.org/mailman/listinfo/python-list

Re: Simple exercise

2016-03-10 Thread BartC
On 11/03/2016 02:03, Mark Lawrence wrote: On 11/03/2016 01:45, BartC wrote: On 11/03/2016 01:21, Mark Lawrence wrote: On 11/03/2016 00:05, BartC wrote: 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]

Re: Simple exercise

2016-03-10 Thread Chris Kaynor
On Thu, Mar 10, 2016 at 4:05 PM, BartC 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 elemen

Re: Simple exercise

2016-03-10 Thread Mark Lawrence
On 11/03/2016 01:56, Martin A. Brown wrote: for i in range(len(names)): print (names[i],totals[i]) Always a code smell when range() and len() are combined. Any other way of traversing two lists in parallel? Yes. Builtin function called 'zip'. https://docs.python.org/3/library/fu

Re: Simple exercise

2016-03-10 Thread Mark Lawrence
On 11/03/2016 01:45, BartC wrote: On 11/03/2016 01:21, Mark Lawrence wrote: On 11/03/2016 00:05, BartC wrote: 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] ? The returns the head of the list. I ne

Re: Simple exercise

2016-03-10 Thread Martin A. Brown
>>> for i in range(len(names)): >>> print (names[i],totals[i]) >> >> Always a code smell when range() and len() are combined. > > Any other way of traversing two lists in parallel? Yes. Builtin function called 'zip'. https://docs.python.org/3/library/functions.html#zip Toy example: im

Re: Simple exercise

2016-03-10 Thread Larry Martell
On Thu, Mar 10, 2016 at 8:45 PM, BartC wrote: > Any other way of traversing two lists in parallel? zip -- https://mail.python.org/mailman/listinfo/python-list

Re: Simple exercise

2016-03-10 Thread BartC
On 11/03/2016 01:21, Mark Lawrence wrote: On 11/03/2016 00:05, BartC wrote: 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] ? The returns the head of the list. I need everything except the last elem

Re: Simple exercise

2016-03-10 Thread Mark Lawrence
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 2

Re: Simple exercise

2016-03-10 Thread BartC
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-Py

Re: Simple exercise

2016-03-10 Thread Chris Angelico
On Fri, Mar 11, 2016 at 10:24 AM, Gregory Ewing wrote: > Instead, pull them out and give them meaningful names. > Doing so with the above code gives: > > name = m.group(1) > value = m.group(2) > if name not in od.keys(): > od[name] = int(value) > else: > od[name] += int(od.get(name

Re: Simple exercise

2016-03-10 Thread Gregory Ewing
Rodrick Brown wrote: 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)) Others have pointed out what's wrong with this, but here's a general tip: Don't repeat complicated subexpressions such as m.group(1

Re: Simple exercise

2016-03-10 Thread Terry Reedy
On 3/10/2016 4:02 AM, 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

Re: Simple exercise

2016-03-10 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote: > od = OrderedDict() This is pointless, then. > […] > od = OrderedDict(map(lambda item: (item[0], 0), items)) > for item in items: od[item[0]] += item[1] -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail

Re: Simple exercise

2016-03-10 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote: [ > key = m.group(1) > value = int(m.group(2)) > > if key not in od: > od[key] = value > else: > od[key] += value > > But there is probably an even more pythonic way to do this. ] For example, based on the orig

Re: Simple exercise

2016-03-10 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote: > key = m.group(1) > value = int(m.group(1)) value = int(m.group(2)) > if key not in od: > od[key] = value > else: > od[key] += value -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bit

Re: Simple exercise

2016-03-10 Thread Thomas 'PointedEars' Lahn
Rodrick Brown wrote: > […] > 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)) > […] This program logic appears to be wrong as you are not adding the value that you just read to the dic

Re: Simple exercise

2016-03-10 Thread Peter Otten
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 > > Howeve

Re: Simple exercise

2016-03-10 Thread Erik
Hi. This looks a little like a homework problem, so I'll be cryptic ... :) On 10/03/16 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 if m: if m.group(1) not

Simple exercise

2016-03-10 Thread Rodrick Brown
>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