我很好奇是否可以执行这样的代码:
在数组中我们有True和False语句,在操作中我们有'AND'或'OR' 或“异或”
def logical_calc(array, op):
if array.count(False)<1 and op=="AND":
return 1
elif array.count(True)>0 and op=='OR':
return 1
elif array.count(True)%2!=0 and op=='XOR':
return 1
else:
return 0
以这种方式:
def logical_calc(array, op):
return True if array.count(False)<1 and op=="AND"
elif array.count(True)>0 and op=='OR'
elif array.count(True)%2!=0 and op=='XOR'
仅在return语句中
答案 0 :(得分:1)
如果三个条件中的任何一个为True,则要返回True,要实现此目的,您要完全消除if语句,并将返回值写为布尔表达式;
def logical_calc(array, op):
return (array.count(False)<1 and op=="AND") or
(array.count(True)>0 and op=='OR') or
(array.count(True)%2!=0 and op=='XOR')