[SciPy-user] Conditionally adding items to a list

Bruce Southey bsouthey@gmail....
Tue Aug 26 10:15:20 CDT 2008


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
>
>   



More information about the SciPy-user mailing list