So I posted a question before, but it was too simplified and rightly got flagged as a duplicate. I'm now posting my problem in more detail so my issue might, hopefully, be resolved. Briefly it is as follows:
I have two lists: a = [10.0,20.0,25.0,40.0]
and b = [1.0,10.0,15.0,20.0,30.0,100.0]
Using list comprehension, I want to exclude from b the ranges of elements specified in a. That is: remove from b all elements between 10.0 and 20.0, and between 25.0 and 40.0. Here is what I tried:
kk = 0
while kk < len(a):
up_lim = a[kk] #upper limit
dwn_lim = a[kk+1] #lower limit
x = [b[y] for y in range(len(b)) if (b[y]<dwn_lim or b[y]>up_lim)] #This line produces correct result if done outside of a while loop. Somehow fails in while loop.
b = list(x) #update the old list with the new&reduced list
kk += 2 #update counter
I'm expecting the result x = [1.0,100.0]
, but I get x = [1.0,10.0,15.0,20.0,30.0,100.0]
In fact, the key line with the list comprehension works if I do it outside the while loop (of course this is useless because list 'a' could be arbitrary in size which is why I used a while loop).
So the question is: how and why does a while loop stop the list comprehension from happening correctly?