Python Set Comprehension

I simplified the test a bit – if all(x%y instead of if not any(not x%y I also limited y’s range; there is no point in testing for divisors > sqrt(x). So max(x) == 100 implies max(y) == 10. For x <= 10, y must also be < x. Instead of generating pairs of primes and … Read more

mean, nanmean and warning: Mean of empty slice

I really can’t see any good reason not to just suppress the warning. The safest way would be to use the warnings.catch_warnings context manager to suppress the warning only where you anticipate it occurring – that way you won’t miss any additional RuntimeWarnings that might be unexpectedly raised in some other part of your code: … Read more

plot a circle with pyplot

You need to add it to an axes. A Circle is a subclass of an Patch, and an axes has an add_patch method. (You can also use add_artist but it’s not recommended.) Here’s an example of doing this: This results in the following figure: The first circle is at the origin, but by default clip_on … Read more

Edit Distance in Python

I’m programming a spellcheck program in Python. I have a list of valid words (the dictionary) and I need to output a list of words from this dictionary that have an edit distance of 2 from a given invalid word. I know I need to start by generating a list with an edit distance of … Read more