Re: Arithmetic with Boolean values

2012-08-14 Thread Hans Mulder
On 12/08/12 22:13:20, Alister wrote: > On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote: > >> On 12/08/2012 17:59, Paul Rubin wrote: which can be simplified to: for x in range(len(L)//2 + len(L)%2): >>> >>> for x in range(sum(divmod(len(L), 2))): ... >>> >>> >> So who's going to b

Re: Arithmetic with Boolean values

2012-08-12 Thread Gene Heskett
On Sunday 12 August 2012 20:27:13 Alister did opine: > On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote: > > On 12/08/2012 17:59, Paul Rubin wrote: > >>> which can be simplified to: > >> > >>> for x in range(len(L)//2 + len(L)%2): > >> for x in range(sum(divmod(len(L), 2))): ... > > > > S

Re: Arithmetic with Boolean values

2012-08-12 Thread Alister
On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote: > On 12/08/2012 17:59, Paul Rubin wrote: >>> which can be simplified to: >>> for x in range(len(L)//2 + len(L)%2): >> >> for x in range(sum(divmod(len(L), 2))): ... >> >> > So who's going to be first in with "and thou shalt not count to 4...

Re: Arithmetic with Boolean values

2012-08-12 Thread Roy Smith
In article , Mark Lawrence wrote: > On 12/08/2012 17:59, Paul Rubin wrote: > >> which can be simplified to: > >> for x in range(len(L)//2 + len(L)%2): > > > > for x in range(sum(divmod(len(L), 2))): ... > > > > So who's going to be first in with "and thou shalt not count to 4..."? You, apparen

Re: Arithmetic with Boolean values

2012-08-12 Thread Mark Lawrence
On 12/08/2012 17:59, Paul Rubin wrote: which can be simplified to: for x in range(len(L)//2 + len(L)%2): for x in range(sum(divmod(len(L), 2))): ... So who's going to be first in with "and thou shalt not count to 4..."? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/

Re: Arithmetic with Boolean values

2012-08-12 Thread Bernd Nawothnig
On 2012-08-12, Paul Rubin wrote: >> which can be simplified to: >> for x in range(len(L)//2 + len(L)%2): > > for x in range(sum(divmod(len(L), 2))): ... nice solution. Bernd -- "Die Antisemiten vergeben es den Juden nicht, dass die Juden Geist haben - und Geld." [Friedrich Nietzsche] -- http

Re: Arithmetic with Boolean values

2012-08-12 Thread Paul Rubin
> which can be simplified to: > for x in range(len(L)//2 + len(L)%2): for x in range(sum(divmod(len(L), 2))): ... -- http://mail.python.org/mailman/listinfo/python-list

Re: Arithmetic with Boolean values

2012-08-12 Thread Roy Smith
In article <502791ea$0$29978$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > for x in (0,) if len(L)%2 else (0, 1): > ... > > which is even more explicit and simpler to read even though it is longer. Ugh. do_stuff() if len(L) % 2 == 0: do_stuff() # reprocess even-length

Re: Arithmetic with Boolean values

2012-08-12 Thread Steven D'Aprano
On Sat, 11 Aug 2012 17:54:40 -0700, Paul Rubin wrote: > John Ladasky writes: [...] >> If the length of the list L is odd, I want to process it once. If >> len(L) is even, I want to process it twice >> for x in range(1 + not(len(L) % 2)): > > If you really have to do something like that, I

Re: Arithmetic with Boolean values

2012-08-11 Thread Paul Rubin
John Ladasky writes: > I have gotten used to switching back and forth between Boolean algebra > and numerical values. Python generally makes this quite easy. Generally ugly though, at least to my tastes. "Explicit is better than implicit" as the saying goes. > If the length of the list L is o

Re: Arithmetic with Boolean values

2012-08-11 Thread MRAB
On 11/08/2012 23:30, John Ladasky wrote: I have gotten used to switching back and forth between Boolean algebra and numerical values. Python generally makes this quite easy. I just found a case that surprises me. Here is what I want to accomplish: I want to process a list. If the length of

Re: Arithmetic with Boolean values

2012-08-11 Thread Chris Angelico
On Sun, Aug 12, 2012 at 10:25 AM, Terry Reedy wrote: > On 8/11/2012 7:13 PM, Chris Angelico wrote: >> This appears to be a limitation of the parser; it's trying to >> interpret "not" as a binary operator. > > I think not. It is lower precedence than all arithmetic operators. > (We humans see that

Re: Arithmetic with Boolean values

2012-08-11 Thread Terry Reedy
On 8/11/2012 7:13 PM, Chris Angelico wrote: On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky wrote: In [7]: 1 + not(len(L) % 2) File "", line 1 1 + not(len(L) % 2) ^ SyntaxError: invalid syntax This appears to be

Re: Arithmetic with Boolean values

2012-08-11 Thread Chris Rebert
On Sat, Aug 11, 2012 at 3:30 PM, John Ladasky wrote: > for x in range(1 + not(len(L) % 2)): > # Do stuff > > This provoked a SyntaxError. I investigated this further with my interpreter > (ipython). > In [5]: not(1) > Out[5]: False > > In [6]: not(len(L) % 2) > Out[6]: False > > In [7]: 1

Re: Arithmetic with Boolean values

2012-08-11 Thread Chris Angelico
On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky wrote: > In [7]: 1 + not(len(L) % 2) > >File "", line 1 > 1 + not(len(L) % 2) >^ > SyntaxError: invalid syntax This appears to be a limitation of the parser; it's trying

Arithmetic with Boolean values

2012-08-11 Thread John Ladasky
I have gotten used to switching back and forth between Boolean algebra and numerical values. Python generally makes this quite easy. I just found a case that surprises me. Here is what I want to accomplish: I want to process a list. If the length of the list L is odd, I want to process it o