蟒蛇冷凝如果声明

0

的问题

我在尝试 如何凝结果的陈述 在我的代码。 我有一个项目,我工作上有几个"如果"的发言(太多的跟踪)和想要找出一种方法凝结。 显然,这涉及一个循环,但我有麻烦了增加额外的行动在这一环。

我想出了 以下工作的例子 证明我的问题:

num=6

if_options = [num==5, num==6]

for i in range(len(if_options)):
    if if_options[i]:
        print(num)

我想添加一个额外的代码。 这个附件将执行一个内操作的,如果声明。 见 下列非工作实例 作为一个框架,为什么我试图完成的:

num=6

if_options = [num==5, num==6]
operations = [num=num+1, num=num-1]

for i in range(len(if_options)):
    if if_options[i]:
        operations[i]
        print(num)

无论出于何种原因,它将不执行的 操作 部分的代码和失败的语法错误。 它不会让我宣布的命令"num=数+1"(无报价)内的一个列表,但是这一《宣言》是必要的,用于执行该命令。 我觉得我缺了一个小小的事情,它应该是一个简单的解决。 谢谢你提前告!!

for-loop function loops python
2021-11-23 22:51:10
4

最好的答案

1

这里的问题是,对操作进行评估时建立的名单。 你想要写他们作为弦,然后 eval/exec 他们在循环。 我会假设你也想的条件进行评估的循环。

num = 6

if_options = ['num==5', 'num==6']
operations = ['num=num+1', 'num=num-1']

for i in range(len(if_options)):
    if eval(if_options[i]):
        exec(operations[i])
        print(num)
2021-11-23 23:04:38

对不起,我改正 evalexec 当运行操作。
Marcel

这一般不是你想要的,但是。 你也许应该实际编写的代码,在你的功能,以及做的重构,一些其他的方式。
Marcel

也许两个具有功能之一用于增加和一种减少会是一个更好的主意。 你可以添加一个参考他们中的一个列表,并呼吁他们在循环。
MSH
0

为什么不能?

def check(inp):
    #you can do some logic and type checking here
    return type(inp)==int # for example, or return arguments to pass to the operatiins

def operation(inp2):
    if inp2: # check if true or not empty, as an example
        #do some operations

# and then you do something like

for x in input_of_things:
    operation( check( x ) )
2021-11-23 23:19:33
0

你可以使用氧表达过。

num = 6
if_option_checks = [lambda x: x == 5, lambda x: x == 6]
operations = [lambda x: x + 1, lambda x: x - 1]
for check, operation in zip(if_option_checks, operations):
    if check(num):
        num = operation(num)

或者你可以使用的字典和lambda表达式

num = 6
if_option_checks = {"add": lambda x: x == 5, "sub": lambda x: x == 6}
operations = {"add": lambda x: x + 1, "sub": lambda x: x - 1}
for key, check in if_option_checks.items():
    if check(num):
        num = operations[key](num)
2021-11-24 00:40:38
0

也许一开关声明的结构可能会有帮助。

第一个定义的开关功能:

def switch(v): yield lambda *c: v in c

然后使用是一个迭代的循环,产生一种情况下功能的交换价值:

for case in switch(num):

    if case(5):
        num = num + 1
        break

    if case(6):
        num = num - 1
        break

    ...
2021-11-24 02:44:41

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................