I have been sweating over this piece of code -- which returns all the primes in a list:
primes = range(2, 20)
for i in range(2, 8):
primes = filter(lambda x: x == i or x % i, primes)
print primes
It works... but I don't understand the role "x == i or x % i
" plays in the whole thing.
I also don't understand why the second range is only 2 to 7.
I even created a Python implementation of the Sieve of Eratosthenes, hoping that might give me some insight, but it didn't.
When I remove the x % i
component, I would expect this code to give me the numbers common to both sets, but it does not:
nums = [2, 20]
for i in range(2, 8):
nums = filter(lambda x: x == i, nums)
print nums
Why is this?
Likewise, when I remove the x == i
component, it returns the prime numbers from 11 to 19.
nums = range(2, 20)
for i in range(2, 8):
nums = filter(lambda x: x % i, nums)
print nums
Again, I don't understand why it ignores all primes below 11.
Next, I tried this:
nums = [13]
for i in range(2, 8):
nums = filter(lambda x: x % i, nums)
print nums
Again, this makes no sense to me. The lambda is iterating over x
in nums
correct? And i
is iterating over the range 2 to 7. So, aren't we taking 13 % i
... for 2 to 7? How does that result in "13"?
Using the same logic as immediately above, I did the same thing with "13", but using x == i
in the lambda.
for i in range(2, 8):
nums = filter(lambda x: x == i, nums)
print nums
And as I expected, it returned an empty list -- which makes sense in my mind, because 13 never appears in the range of 2 to 7.
For reference to anyone trying to help, this is the mindset I'm in when I work with filter()
and lambdas:
a = range (1, 11)
b = range (9, 20)
for i in filter(lambda x: x in a, b):
print i,
This, of course, gives us "9 10". I know the structure of the loop is different, but hopefully it will help you see where my confusion lies.
I have worked with filter()
and lambdas somewhat extensively, so I thought I could figure it out, but I'm stumped! I just hope the answer isn't so blindingly obvious that I feel like an idiot...