我在python中有一个列表,应该如下所示,例如:
line = ['0', '1', '0', 'R', '1']
我写了这个函数,但是可以用更简单的方法吗?你知道吗
def checkCardCommands(line):
if line[0] == '0' or line[0] == '1':
if line[1] == '0' or line[1] == '1' or line[1] == 'None':
if line[2] == '0' or line[2] == '1' or line[2] == 'None':
if line[3] == 'R' or line[3] == 'L':
if line[4] == '0' or line[4] == '1':
if len(line) == 5:
return True
else:
return False
else:
return False
else:
return False
else:
return False
else:
return False
else:
return False
你可以这样写:
更简短,更易于阅读和维护。你知道吗
如果您的验证不太复杂,您可以自己编写一些验证帮助程序:
有关更高级的数据结构模式验证,请参阅voluptuous。你知道吗
相关问题 更多 >
编程相关推荐