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
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):
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
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
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
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
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
> "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
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
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
>> [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
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
>
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 ..
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
> 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
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
16 matches
Mail list logo