【问题标题】:How to switch between multiple variables using an if-else statement in Python如何在 Python 中使用 if-else 语句在多个变量之间切换
【发布时间】:2020-01-31 22:36:04
【问题描述】:

如何使用单个开关变量在多个变量之间进行切换?

更新: 澄清意图是在这两组变量之间无限次切换。

当我尝试这个时,我得到以下错误。

a1= 'process1'
a2 = 'process2'

b1 = 'action1'
b2 = 'action2'

switch = True # the switch to indicate which set of variables to use
N = 10        # the number of times to switch between the two sets of variables

# alternate between two sets of variables N times
for i in range (N):
    active_process, active_action = a1, b1 if switch else a2, b2

    print("active_process: %s, active_action is: %s" %(active_process, active_action))
    switch = not switch

追溯:

Traceback (most recent call last):
  File "/home/username/.PyCharm2019.3/config/scratches/scratch_10.py", line 10, in <module>
    active_process, active_action = a1, b1 if switch else a2, b2
ValueError: too many values to unpack (expected 2)

Process finished with exit code 1

【问题讨论】:

  • 这里不需要iswitchfor response, greeting in (a1, b1), (a2, b2):
  • 或者更习惯用法:responses = ['yes', 'no']; greetings = ['hello', 'goodby']; for response, greeting in zip(responses, greetings):
  • 感谢您的帮助。我更新以更清楚地说明问题的意图。两组变量之间的切换时间不定。
  • 我将 that 写为for ap, aa in islice(cycle([(a1,b1), (a2,b2)]), N):(其中islicecycle 是从itertools 导入的)。或者,如果您需要/想要ifor i, (ap, aa) in zip(range(N), cycle(...))
  • 错误来自优先级问题:(a1, b1) if switch else (a2, b2).

标签: python if-statement boolean


【解决方案1】:

你把它弄得太脆弱了。您有一个问候/响应值表和一个布尔值,告诉您使用哪个。只需使用直接访问列表即可:

table = [ ("process1", "action1"),
          ("process2" , "action2")
        ]

N = 10
for i in range(10):
    print("%s, the answer is: %s" % table[i %2])

或者,使用字典:

table = { True:  ("process1", "action1"),
          False: ("process2" , "action2")
        }
N = 10
for i in range(N):
    print("%s, the answer is: %s" % table[i %2])

【讨论】:

  • 目的是在两组变量之间无限次切换。如果您使用模 i%2 来引用表列表的索引,这很容易实现。
  • 是的。我回答了发布的问题;你后来更新了。
  • 唯一的变化是更新变量,这样它们就不会像问题的初衷那样误导,并将交替的次数从 2 更改为 10。你介意我用 i%2 作为表的索引来更新你的答案吗?
  • 谢谢@Prune 我不想显得我不欣赏你。你说得对,桌子更健壮!
【解决方案2】:

使用列表来打包和拆包物品得到了预期的结果:

a1= 'process1'
a2 = 'process2'

b1 = 'action1'
b2 = 'action2'

switch = True # the switch to indicate which set of variables to use
N = 10        # the number of times to switch between the two sets of 

# alternate between two sets of variables N times
for i in range (N):
    [active_process, active_action] = [a1, b1] if switch else [a2, b2]

    print("active_process: %s, active_action is: %s" %(active_process, active_action))
    switch = not switch

输出:

active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2
active_process: process1, active_action is: action1
active_process: process2, active_action is: action2

Process finished with exit code 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-29
    • 2013-01-07
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多