或者使用reduce
def is_prime_no(x): #"True" represents Prime
np = 1
if x < 2:
np = 0
else:
for i in range(3, x): #int(math.sqrt(n))
if x % i == 0:
np =0
return np
print is_prime_no(12)
def prime_check(a,b):
if is_prime_no(a) == 1 and is_prime_no(b) == 1:
return 1
else:
return 0
print "prime check result ", prime_check(13,17)
从这里起作用
def list_prime_check(values):
return reduce(prime_check, values)
print "Check items in list are prime ", list_prime_check([13,17,19])
返回0但不是1 - 我是真的
答案 0 :(得分:0)
请记住,在reduce(lambda a, b: ..., ...)
中,函数的返回值将成为新的a
。因此,你有:
reduce(prime_check, [13, 17, 19]) # =>
prime_check(13, 17) # => 1 =>
reduce(prime_check, [1, 19]) # uhh, what? =>
prime_check(1, 19) # uh-oh...
使用all
all(...)
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
像这样使用:
>>> all([is_prime_no(x) for x in [13, 17, 19]])
True