Lar*_*sus 2 python algorithm if-statement code-readability
我有以下情况:
string = "abc"
if (string[1] == "b" and string[2] == "c") or (string[1] == "c" and string[2] == "b"):
print("ERROR")
Run Code Online (Sandbox Code Playgroud)
是否有解决方案以pythonic方式缩短此时间?我看到这(string[1] == "b" and string[2] == "c")是的相反陈述(string[1] == "c" and string[2] == "b")。也许我可以使用?
是否有解决方案以pythonic方式缩短此时间?
是的,这里是:
string = "abc"
if (string[1:3] == "bc") or (string[1:3] == "cb"):
print("ERROR")
Run Code Online (Sandbox Code Playgroud)
如果渴望更短的时间- if string[1:3] in ('bc', 'cb'):