简体   繁体   English

如何对多个 if elif 和 else 语句使用 python 列表推导式

[英]How to use python list comprehension for multiple if elif and else statements

I am trying to write using list comprehensions for the scenario.我正在尝试使用列表理解来编写场景。

I want fizzbuzz if it is divisible by both 3 and 5;如果它可以被 3 和 5 整除,我想要 fizzbuzz; fizz for divisble by 3 and buzz if divisible by 5. fizz 表示可被 3 整除,buzz 表示可被 5 整除。

It seems like in my logic I have to use else logic in the end using '' to get the output.似乎在我的逻辑中,我最终必须使用 else 逻辑使用 '' 来获取输出。 if I don't use the last else I am getting an error.如果我不使用最后一个 else 我会收到错误。 I am sure I am missing something silly.我确定我错过了一些愚蠢的东西。

['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz'if i%5==0 else ''   for i in range(1,51) ]

Sorry, I didn't read the question completely;抱歉,我没有完全阅读问题; if none of the scenarios match then we need to print the number.如果没有一个场景匹配,那么我们需要打印数字。 so the solution I wrote needed an 'i' to print in the default scenarios.所以我写的解决方案需要一个“i”来在默认场景中打印。

print(['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz'if i%5==0 else i   for i in range(1,51) ])

Note: this answer was created before OP changed their question, so you might need to tweak the code you use.注意:这个答案是在 OP 更改他们的问题之前创建的,因此您可能需要调整您使用的代码。 The process that you use will be very similar to the write-up below, so I figure I should keep this answer posted for now.您使用的过程将与下面的文章非常相似,所以我想我现在应该保留这个答案。

As a disclaimer, this answer is more from a code golf perspective than a practical perspective.作为免责声明,这个答案更多地是从代码高尔夫的角度而不是实际的角度。 Don't try to use this approach an actual project (or even a job interview) because your code will be too confusing to understand.不要尝试在实际项目(甚至是工作面试)中使用这种方法,因为您的代码会太混乱而无法理解。

First, we need something to test our modifications against, because these modifications can be very error-prone.首先,我们需要一些东西来测试我们的修改,因为这些修改很容易出错。 Feel free to ignore this, it's just so we can compare the list comprehensions we develop to the expected output:随意忽略这一点,它只是为了将我们开发的列表推导式与预期输出进行比较:

list(filter(None, ['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz'if i%5==0 else '' for i in range(1,51)]))

Now, let's try to make the comprehension shorter :)现在,让我们尝试使理解更短:)

You can apply a filter operation when constructing lists by placing an if statement on the right side of the range statement:通过在 range 语句的右侧放置一个 if 语句,您可以在构建列表时应用过滤操作:

['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz' for i in range(1,51) if (i%5==0 or i%3 == 0)]

It seems like you are trying to optimize for code length (which can be pretty fun sometimes so I totally get it).看起来您正在尝试优化代码长度(有时这可能很有趣,所以我完全理解)。 If this is the case, we can further reduce your comprehension.如果是这种情况,我们可以进一步降低您的理解力。

First, Python evaluates 1 as true and 0 as false.首先,Python 将 1 评估为真,将 0 评估为假。 This allows us to re-write (i%3==0 and i%5==0) as (not i%3 and not i%5) .这允许我们将(i%3==0 and i%5==0)重写为(not i%3 and not i%5)

Next, we can apply DeMorgan's Law:接下来,我们可以应用德摩根定律:

(not i%3 and not i%5) EQUALS not (i%3 or i%5) . (not i%3 and not i%5)等于not (i%3 or i%5)

By applying this discovery we can get a decent character reduction in our original statement, which is pretty neat:通过应用这个发现,我们可以在原始语句中得到一个不错的字符减少,这非常简洁:

['fizzbuzz' if not (i%3 or i%5) else 'fizz' if i%3==0 else 'buzz' for i in range(1,51) if not (i%5 and i%3)]

Testing against the original spec we set up at the top:针对我们在顶部设置的原始规范进行测试:

list(filter(None, ['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz'if i%5==0 else '' for i in range(1,9999)])) == ['fizzbuzz' if not (i%3 or i%5) else 'fizz' if i%3==0 else 'buzz' for i in range(1,9999) if not (i%5 and i%3)]
# evaluates to True :)

Another trick we can use to clean things up is the fact that Python allows us to multiply strings by integers (try in in interactive mode and see what happens):我们可以用来清理的另一个技巧是 Python 允许我们将字符串乘以整数(尝试在交互模式下看看会发生什么):

['fizz'*(i%3==0) + 'buzz'*(i%5==0) for i in range(1,51) if not (i%5 and i%3)]

Testing against the original spec we set up at the top:针对我们在顶部设置的原始规范进行测试:

list(filter(None, ['fizzbuzz' if (i%3==0 and i%5==0) else 'fizz' if i%3==0 else 'buzz'if i%5==0 else '' for i in range(1,9999)])) == ['fizz'*(i%3==0) + 'buzz'*(i%5==0) for i in range(1,9999) if not (i%5 and i%3)]
# evaluates to True :)

If anybody can reduce the line count any further (without just shortening variables/whitespace), feel free to edit this answer, since that seems to be the goal for this question.如果有人可以进一步减少行数(而不仅仅是缩短变量/空格),请随时编辑此答案,因为这似乎是此问题的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM