des*_*r87 3 python data-structures
如何从元组中获取其他(不匹配的值)?
例如:我有 val = 'y'
和 t = ('y', 'n')
我想'n'
从元组中返回.就像是
if val=='y':
return 'n'
else:
retun 'y'
Run Code Online (Sandbox Code Playgroud)
您可以使用以下语句:
return t[0] if val == 'y' else t[1]
Run Code Online (Sandbox Code Playgroud)
您还可以索引到元组:
return t[val == 'n']
Run Code Online (Sandbox Code Playgroud)