假设你的例子如上述,在很长的路要走(每片的解释如下):
pos = max(l.find(':'), l.find('='), 0)
print(l[pos:].strip())
下面就来缩短到一条线的方式,用它在真实评价的顺序各部分的说明。
print(l[max(l.find(':'),l.find('='),0):].strip())
#--------------- Breakdown
# max -> highest of values; find returns -1 if it isn't there.
# using a 0 at the end means if ':'/'=' aren't in the string, print the whole thing.
# l.find(),l.find() -> check the two characters, using the higher due to max()
# l[max():] -> use that higher value until the end (implied with empty :])
# .strip() -> remove whitespace
'IND = l.index( “:”);如果ind == -1:ind = l.index(“=”); print(l [pos + 1:])'或者非常可能'print(l [max([l.index(“:”),l.index(“=”)])+ 1:])'这实际上是不可读的。点是 - 你需要将'.index()'分成两个单独的测试 – dwanderson
''''或'=''评估为'':'' –