我正打算打印出找到的素数。我想添加到prime [],但我得到一个TypeError
line 63, in isprime
primes += n
TypeError: 'int' object is not iterable
代码:
def isprime(n):
primes = []
if n == 1:
print '1 is special'
return False
for x in range(2, n):
if n%x == 0:
print '{} equals {} x {}'.format(n, x, n // x)
return False
else:
primes += n
print (n, "is a prime number")
return True
for n in range(1, 1000):
isprime(n)
答案 0 :(得分:4)
+=
上的 list
旨在将list
连接到另一个list
。如果你想在primes.append(n)
的末尾添加一个元素,你可以直接做到:
list
或制作一个临时.append
以允许列表连接操作工作(这种方法较慢,并且只是更简洁,涉及更大的内存流失;我强烈建议使用+=
除非你需要要一次添加多个元素,在这种情况下,primes += [n]
方法可以更好地扩展多元素列表文字):
func sFunc_imageFixOrientation(img:UIImage) -> UIImage {
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform:CGAffineTransform = CGAffineTransformIdentity
// Rotate image clockwise by 90 degree
if (img.imageOrientation == UIImageOrientation.Up) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.Down
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
}
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
}
if (img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.UpMirrored
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformScale(transform, -1, 1)
}
if (img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(img.size.width), Int(img.size.height),
CGImageGetBitsPerComponent(img.CGImage), 0,
CGImageGetColorSpace(img.CGImage),
CGImageGetBitmapInfo(img.CGImage).rawValue)!;
CGContextConcatCTM(ctx, transform)
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored
|| img.imageOrientation == UIImageOrientation.Up
) {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
} else {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
}
// And now we just create a new UIImage from the drawing context
let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
let imgEnd:UIImage = UIImage(CGImage: cgimg)
return imgEnd
}
}
答案 1 :(得分:0)
primes
是一个列表,因此要向其添加n
int
,您必须使用append
方法。将primes += n
替换为primes.append(n)
。