Python 中集合的模运算符
Modulo Operator on a set in Python
如何对一组数字使用取模运算符?
if value > 2 and value % 2 == 0 or value % 3 == 0 or value % 5 == 0 or value % 7 == 0 or value % 11 == 0:
return False
如何将所有这些“或”语句合并为更优雅的语句,如“and value % set == 0
”?
您可以使用 any(...)
:
value = 100
if value > 2 and (any(value % x == 0 for x in [2, 3, 5, 7, 11])):
print(False)
使用any
其中
Return True if bool(x) is True for any x in the iterable
checks = {2, 3, 5, 7, 9}
if value > 2 and any(value % check == 0 for check in checks):
return False
numbers = [2, 3, 5, 7, 11]
value = #define value here
def modulo():
for number in numbers:
if value > 2:
if value % number == 0:
return False
modulo()
试试这个:
def Fun(value):
if value > 2 and any([value%x==0 for x in [2, 3, 5, 7, 11]]):
return False
return True
如何对一组数字使用取模运算符?
if value > 2 and value % 2 == 0 or value % 3 == 0 or value % 5 == 0 or value % 7 == 0 or value % 11 == 0:
return False
如何将所有这些“或”语句合并为更优雅的语句,如“and value % set == 0
”?
您可以使用 any(...)
:
value = 100
if value > 2 and (any(value % x == 0 for x in [2, 3, 5, 7, 11])):
print(False)
使用any
其中
Return True if bool(x) is True for any x in the iterable
checks = {2, 3, 5, 7, 9}
if value > 2 and any(value % check == 0 for check in checks):
return False
numbers = [2, 3, 5, 7, 11]
value = #define value here
def modulo():
for number in numbers:
if value > 2:
if value % number == 0:
return False
modulo()
试试这个:
def Fun(value):
if value > 2 and any([value%x==0 for x in [2, 3, 5, 7, 11]]):
return False
return True