[SciPy-user] Array selection help

josef.pktd@gmai... josef.pktd@gmai...
Wed Feb 11 13:06:20 CST 2009


On Wed, Feb 11, 2009 at 1:54 PM, Andrew Straw <strawman@astraw.com> wrote:
>
>> def labelmeanfilter(arr1, arr2):
>>     R = {}
>>     [R.setdefault(row[0],[]).append(index) for index, row in
>>                      enumerate(zip(arr1,arr2))]
>>
>
>
> I think the following should produce a bit more speed (although I
> haven't benchmarked it) because it avoids creating len(arr1) empty lists.
>
> import collections
>
> def labelmeanfilter(arr1, arr2):
>    R = collections.defaultdict(list)
>    [R[row[0]].append(index) for index, row in
>                     enumerate(zip(arr1,arr2))]
>

Yes, its around 14% faster for the example, however, it requires
python 2.5 and  I am still thinking as if I were using 2.4.

Josef


More information about the SciPy-user mailing list