[SciPy-user] Conditionally adding items to a list
Roger Herikstad
roger.herikstad@gmail....
Tue Aug 26 11:31:59 CDT 2008
Hi,
My criterion is that no two elements can be closer than a given
threshold, in this case 1000. I am aware that, depending on the
sorting of the lists, the order would matter, and that's why I need to
check each added element against all previously added elements, to
make sure no violations occur. I found David's code to substantially
faster than what I wrote, and the only thing I need to correct for is
that fact that the difference matrix could exceed my available memory.
That being said, I will definitely look into the other examples you
mentioned. Thanks!
~ Roger
On Tue, Aug 26, 2008 at 11:15 PM, Bruce Southey <bsouthey@gmail.com> wrote:
> Roger Herikstad wrote:
>> Hi all,
>> I have a prolem that I was wondering if anyone could come up with a
>> good solution for. I basically have to lists of number and I want to
>> add elements from one list to the other as long as the difference
>> between the added element and all elements already in that list
>> exceeds a certain threshold.
> Can you please explain with an example which 'difference' you want? Do
> you mean the minimum, maximum, sum, average etc. of all elements in that
> list? Or even that the sum of differences is smaller than a certain
> threshold?
>
> Alternative, does the threshold vary as elements are added? You really
> need to be careful here because the criterion will change depending on
> the order that the elements are added and that not all elements within
> the final list meet the criterion used to create it (as well elements
> excluded that should have been in the list if the list was sorted
> differently).
>
> If it just depends on the first list, then you can use the 'where'
> function or boolean indexing to identify and extract the elements such
> as t2[numpy.abs(t2>(min(t1)+1000))] which can then be append to the
> times1 list (note times1 does not need to be converted to an array
> unless you actually need it as an array). I do recommend exploring the
> example web page on this as it has very informative examples on what
> different approaches actually work.
>
> If it depends on both lists hopefully there is a conditional approach
> where you can apply your criterion to one list first and then the other
> that does not depend on the order entered:
> threshold=min(numpy.min(t1),numpy.min(t2))+1000
> t2[numpy.abs(t2>threshold)]
>
>
> Bruce
>> The code I came up with is
>>
>> map(times1.append,ifilter(lambda(x):
>> numpy.abs(x-numpy.array(times1)).min()>1000, times2))
>>
>> but it quickly slows down if times2 becomes sufficiently long. I need
>> to be able to do this with lists of 100,000++ elements. Does anyone
>> know of a quicker way of doing this?
>>
>> Thanks!
>>
>> ~ Roger
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user@scipy.org
>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>
>>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-user
mailing list