gpt4 book ai didi

python - 用于更改多个变量的高效 if-elif

转载 作者:太空宇宙 更新时间:2023-11-04 05:11:24 25 4
gpt4 key购买 nike

<分区>

我有几个变量,其中一些我需要在特定条件下更改。

test = 'foo'
a, b, c = None, None, None
if test == 'foo':
a = 1
elif test == 'bar':
b = 2
else:
c = 3

我想使用 here 中描述的字典方法,但如何修改它以更改多个变量?我希望它像这样工作:

options = {'foo': ('a',1), 'bar': ('b',2)}
reassign_variables(options,test, ('c',3))

或者如果不创建一个函数并单独对所有条件进行硬编码,就不能做到这一点吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com