在下面的代码中,我使用集合推导来计算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)
答案 0 :(得分: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)
答案 1 :(得分:0)
在您的示例中,noPrimes
和primes
都已设置,并且设置是无序的,从文档中可以明显看出: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'>
答案 2 :(得分:0)
@ 101arrowz在他的评论中提供了正确答案
集是无序的。您将不会总是获得序列化的输出。如果要按顺序使用列表理解,则应该使用
为扩展该答案,您可能会看到事物在一定时间内排序的一个原因是,某些整数在运行时之前已预先填充,从而为它们提供了确定性的ID顺序(对于我来说,其值为-5,256,以下为2.7和3.6 cPython)。
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
请注意Is it better to use "is" or "==" for number comparison in Python?
中的其他详细信息