Re: [Tutor] Aschenputtel problem

2005-09-16 Thread Orri Ganel
Christopher Arndt wrote: >Hi, > >I wonder if there is a shorter form of the following idiom: > >list1 = [] >list2 = [] >for item in original_list: >if condition(item): >list1.append(item) >else: >list2.append(item) > >(optional step:) > >original_list[:] = list1 > > >I call

Re: [Tutor] Aschenputtel problem

2005-09-16 Thread bob
At 10:12 AM 9/15/2005, Christopher Arndt wrote: >Hi, > >I wonder if there is a shorter form of the following idiom: > >list1 = [] >list2 = [] >for item in original_list: > if condition(item): > list1.append(item) > else: > list2.append(item) Consider (5 lines instead of 7):

Re: [Tutor] Aschenputtel problem - Shorter is not better?

2005-09-16 Thread Christopher Arndt
Pierre Barbier de Reuille schrieb: > Well, I have some comments ^_^ As should be always expected, when it comes to benchmarking ;-) > First, about the function 6, it is exactly equivalent (and a little bit > quicker) to use: > [...] > That is, put the try block outside the loop ... however, if th

Re: [Tutor] Aschenputtel problem - Shorter is not better?

2005-09-16 Thread Pierre Barbier de Reuille
Well, I have some comments ^_^ First, about the function 6, it is exactly equivalent (and a little bit quicker) to use: ===8<==8<==8<==8<==8<==8<==8<==8<==8<=== def cinderella6(original_list, condition): """Using while and list popping.""" list1 = [] i

Re: [Tutor] Aschenputtel problem - Shorter is not better?

2005-09-16 Thread Christopher Arndt
Terry Kemmerer schrieb: > > > "Bearing in mind that shorter is not necessarily better..." > > Wouldn't shorter, as a rule of thumb, as in less language statements, mean > fewer > executions, and therefore faster? Well, see for yourself... I wrote a little benchmarking script for different so

Re: [Tutor] Aschenputtel problem

2005-09-16 Thread Christopher Arndt
Pierre Barbier de Reuille schrieb: > Well, in the specific case of numeric arrays, you can use Numeric or > numarray : > > from Numeric import array, compress > > ol = array(original_list) > selection = array([condition(i) for i in original_list) > list1 = compress(selection, ol) > list2 = compre

Re: [Tutor] Aschenputtel problem

2005-09-16 Thread Pierre Barbier de Reuille
Well, in the specific case of numeric arrays, you can use Numeric or numarray : from Numeric import array, compress ol = array(original_list) selection = array([condition(i) for i in original_list) list1 = compress(selection, ol) list2 = compress(!selection, ol) Even better, if condition can be

Re: [Tutor] Aschenputtel problem - Shorter is not better?

2005-09-16 Thread Alan G
> "Bearing in mind that shorter is not necessarily better..." > > Wouldn't shorter, as a rule of thumb, as in less language > statements, mean fewer > executions, and therefore faster? Only in a very general sense. In practice a complex expression (such as a list comprehension or a regular expres

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Bob Gailer
At 10:12 AM 9/15/2005, Christopher Arndt wrote: Hi,I wonder if there is a shorter form of the following idiom:list1 = []list2 = []for item in original_list:    if condition(item):    list1.append(item)    else:    list2.append(item)Consider (5 lines instead of 7):lists = [[], []]for it

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Christopher Arndt
Alan G schrieb: > My personal approach would be two separate comprehensions(*) which > keeps the intent very clear and keeps it short at the expense > of iterating the original list twice. Of course this works only if condition(item) yields the same result each time it is called with the same in

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Alan G
>> [condition(i) and list1.append(i) or list2.append(i) for i in >> original] > > Hmm, no, this will always evaluate list2.append(i) because > the value of list1.append(i) is None. You could use > > [ (condition(i) and list1 or list2).append(i) for i in original ] Oops! Good point, which empha

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread John Fouhy
On 16/09/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Alan Gauld wrote: > > Bearing in mind that shorter is not necessarily better... > > > > [condition(i) and list1.append(i) or list2.append(i) for i in > > original] > > Hmm, no, this will always evaluate list2.append(i) because the value of >

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Roel Schroeven
Alan Gauld schreef: >>I wonder if there is a shorter form of the following idiom: > > > Bearing in mind that shorter is not necessarily better... > > [condition(i) and list1.append(i) or list2.append(i) for i in > original] Alas, won't work. This is one of the cases where the ... and ... or ..

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Kent Johnson
Alan Gauld wrote: >>I wonder if there is a shorter form of the following idiom: > > > Bearing in mind that shorter is not necessarily better... > > [condition(i) and list1.append(i) or list2.append(i) for i in > original] Hmm, no, this will always evaluate list2.append(i) because the value of

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Alan Gauld
> I wonder if there is a shorter form of the following idiom: Bearing in mind that shorter is not necessarily better... [condition(i) and list1.append(i) or list2.append(i) for i in original] This returns a list of booleans that we throw away but the list1,list2 containers will have been modifi

Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Kent Johnson
Christopher Arndt wrote: > Hi, > > I wonder if there is a shorter form of the following idiom: > > list1 = [] > list2 = [] > for item in original_list: > if condition(item): > list1.append(item) > else: > list2.append(item) I don't think so. You can write it as two list c