Python集合理解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21770885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Set Comprehension
提问by user3308790
So I have these two problems for a homework assignment and I'm stuck on the second one.
所以我在家庭作业中遇到了这两个问题,我被第二个问题困住了。
Use a Python Set Comprehension (Python's equivalent of Set Builder notation) to generate a set of all of the prime numbers that are less than 100. Recall that a prime number is an integer that is greater than 1 and not divisible by any integer other than itself and 1. Store your set of primes in a variable (you will need it for additional parts). Output your set of primes (e.g., with the print function).
Use a Python Set Comprehension to generate a set of ordered pairs (tuples of length 2) consisting of all of the prime pairs consisting of primes less than 100. A Prime Pair is a pair of consecutive odd numbers that are both prime. Store your set of Prime Pairs in a variable. Your set of number 1 will be very helpful. Output your Set of Prime Pairs.
使用 Python Set Comprehension(Python 相当于 Set Builder 表示法)生成所有小于 100 的素数的集合。回想一下,素数是一个大于 1 且不能被除 1 以外的任何整数整除的整数本身和 1. 将您的素数集存储在一个变量中(您将需要它来存储其他部分)。输出您的一组素数(例如,使用打印功能)。
使用 Python Set Comprehension 生成一组有序对(长度为 2 的元组),其中包含由小于 100 的素数组成的所有素数对。素数对是一对连续的奇数,它们都是素数。将您的一组质数对存储在一个变量中。您的第 1 组将非常有帮助。输出您的一组质数对。
For the first one, this works perfectly:
对于第一个,这非常有效:
r= {x for x in range(2, 101)
if not any(x % y == 0 for y in range(2, x))}
However, I'm pretty stumped on the second one. I think I may have to take the Cartesian product of the set r with something but I'm just not sure.
然而,我对第二个感到很困惑。我想我可能不得不用一些东西来获取集合 r 的笛卡尔积,但我不确定。
This gets me somewhat close but I just want the consecutive pairs.
这让我有点接近,但我只想要连续的对。
cart = { (x, y) for x in r for y in r
if x < y }
采纳答案by Hugh Bothwell
primes = {x for x in range(2, 101) if all(x%y for y in range(2, min(x, 11)))}
I simplified the test a bit - if all(x%yinstead of if not any(not x%y
我简化了测试 -if all(x%y而不是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.
我也限制了 y 的范围;测试除数 > sqrt(x) 没有意义。所以 max(x) == 100 意味着 max(y) == 10。对于 x <= 10,y 也必须 < x。
pairs = {(x, x+2) for x in primes if x+2 in primes}
Instead of generating pairs of primes and testing them, get one and see if the corresponding higher prime exists.
与其生成素数对并测试它们,不如获取一个并查看相应的更高素数是否存在。
回答by icedtrees
You can generate pairs like this:
您可以像这样生成对:
{(x, x + 2) for x in r if x + 2 in r}
Then all that is left to do is to get a condition to make them prime, which you have already done in the first example.
然后剩下要做的就是获得一个条件,使它们成为质数,这在第一个示例中已经完成。
A different way of doing it: (Although slower for large sets of primes)
一种不同的方法:(虽然对于大型素数集较慢)
{(x, y) for x in r for y in r if x + 2 == y}
回答by Raymond Hettinger
You can get clean and clear solutions by building the appropriate predicates as helper functions. In other words, use the Python set-builder notation the same way you would write the answer with regular mathematics set-notation.
您可以通过构建适当的谓词作为辅助函数来获得干净清晰的解决方案。换句话说,使用 Python 集合构建器表示法的方式与使用常规数学集合表示法编写答案的方式相同。
The whole idea behind set comprehensions is to let us write and reason in code the same way we do mathematics by hand.
集合推导式背后的整个想法是让我们以与手工数学相同的方式编写和推理代码。
With an appropriate predicate in hand, problem 1 simplifies to:
有了适当的谓词,问题 1 可以简化为:
low_primes = {x for x in range(1, 100) if is_prime(x)}
And problem 2 simplifies to:
问题 2 简化为:
low_prime_pairs = {(x, x+2) for x in range(1,100,2) if is_prime(x) and is_prime(x+2)}
Note how this code is a direct translation of the problem specification, "A Prime Pair is a pair of consecutive odd numbers that are both prime."
请注意此代码如何直接翻译问题说明,“质数对是一对连续的奇数,它们都是质数”。
P.S. I'm trying to give you the correct problem solving technique without actually giving away the answer to the homework problem.
PS 我正在尝试为您提供正确的问题解决技术,而实际上并没有给出家庭作业问题的答案。

