-3

我在下面的代码中使用集合理解来计算 2 到 n 之间的素数,n=132。

只要变量 n 的值 <= 131,生成的素数就会以正确的升序打印,即 {2,3,5,7,11,...}。

每当 n > 131 时,打印顺序就会倾斜,例如 {2,3,131,5,7,...}。

无论“n”的值如何,变量“noPrimes”的值总是以正确的顺序打印。

我不太明白为什么?

Environment: Python: 3.7.2, macOS: Mojave 10.14.4, IDE: WingPro version 7.0.1.2
Code: 

    from math import sqrt
    n = 132
    sqrt_n = int (sqrt(n))
    noPrimes = {j for i in range (2, (sqrt_n + 1)) for j in range (i*2, n, i)}
    primes = {x for x in range (2,n) if x not in noPrimes}
    print ("Printing 'noPrimes':")
    print (noPrimes)
    print ("Printing 'Primes':")
    print (primes)

4

3 回答 3

0
from math import sqrt
n = 132
sqrt_n = int (sqrt(n))
noPrimes = {j for i in range (2, (sqrt_n + 1)) for j in range (i*2, n, i)}
primes = [x for x in range (2,n) if x not in noPrimes]
print ("Printing 'noPrimes':")
print (sorted(noPrimes))
print ("Printing 'Primes':")
print (primes)
于 2019-05-02T03:17:59.400 回答
0

在您的示例中,两者noPrimesprimes都已设置,并且 set 是无序的,从文档中可以看出:https ://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

集合对象是不同的可散列对象的无序集合。

from math import sqrt
n = 132
sqrt_n = int (sqrt(n))
noPrimes = {j for i in range (2, (sqrt_n + 1)) for j in range (i*2, n, i)}
primes = {x for x in range (2,n) if x not in noPrimes}
print ("Printing 'noPrimes':")
print (noPrimes)
print ("Printing 'Primes':")
print (primes)
print(type(noPrimes))
print(type(primes))

所以原始情况下的输出将是 .

Printing 'noPrimes':
{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130}
Printing 'Primes':
{2, 3, 131, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127}
<class 'set'>
<class 'set'>

所以你得到的答案是有效的,只是它们没有顺序

如果你想要订购,你想做的就是这样的列表理解

from math import sqrt
n = 132
sqrt_n = int (sqrt(n))
#noPrimes is a list
noPrimes = [j for i in range (2, (sqrt_n + 1)) for j in range (i*2, n, i)]
#primes is a list 
primes = [x for x in range (2,n) if x not in noPrimes]
print ("Printing 'noPrimes':")
print (noPrimes)
print ("Printing 'Primes':")
print (primes)
print(type(noPrimes))
print(type(primes))

然后输出将是

Printing 'noPrimes':
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120, 126, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121]
Printing 'Primes':
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131]
<class 'list'>
<class 'list'>
于 2019-05-02T03:35:42.433 回答
0

@101arrowz 在他的评论中提供了正确答案

集合是无序的。你不会总是得到序列化的输出。如果您希望它们按顺序排列,您应该使用列表理解

为了扩展该答案,您可能会看到在一定时间内订购的东西的一个原因是,一些整数是在运行前预先填充的,给它们一个确定的 id 顺序(下面我使用 2.7 和 3.6 cPython 时为 -5,256)。

def collapse(items):
    ret = []
    if not items:
        return ret
    first = last = items[0]
    for i in items[1:]:
        if i == last + 1:
            last = i
        else:
            ret.append((first, last))
            first = last = i
    ret.append((first, last))
    return ret


def pretty_collapse(items):
    vals = collapse(items)
    return ', '.join('%d..%d' % (a, b) if a != b else '%d' % a
                     for a, b in vals)


int_by_id = list(sorted(range(-50, 300), key=id))
print(pretty_collapse(int_by_id))

-5..256, -48, -49, -50, -32, -47, -36, -37, -42..-41, -43, -33, -34, -35, -44, -45, -46, -38, -39, -40, -24, -25, -26, -27, -28, -29, -30, -31, -23..-6, 257..299

请注意在 Python 中使用“is”或“==”进行数字比较是否更好?

于 2019-05-02T03:36:33.670 回答