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. I know the structure of the loop is different, but hopefully it will help you see where my confusion lies. for i in range(2, 8):Īnd 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.įor reference to anyone trying to help, this is the mindset I'm in when I work with filter() and lambdas: a = range (1, 11) ![]() ![]() Using the same logic as immediately above, I did the same thing with "13", but using x = i in the lambda. for 2 to 7? How does that result in "13"? ![]() The lambda is iterating over x in nums correct? And i is iterating over the range 2 to 7. nums = range(2, 20)Īgain, I don't understand why it ignores all primes below 11.Īgain, this makes no sense to me. Likewise, when I remove the x = i component, it returns the prime numbers from 11 to 19. 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 = I even created a Python implementation of the Sieve of Eratosthenes, hoping that might give me some insight, but it didn't. I also don't understand why the second range is only 2 to 7. ![]() but I don't understand the role " x = i or x % i" plays in the whole thing. Primes = filter(lambda x: x = i or x % i, primes) I have been sweating over this piece of code - which returns all the primes in a list: primes = range(2, 20)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |