Python if else 在列表理解中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4406389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:35:28  来源:igfitidea点击:

if else in a list comprehension

pythonlistlist-comprehensionif-statement

提问by user225312

I have a list l:

我有一个清单l

l = [22, 13, 45, 50, 98, 69, 43, 44, 1]

For numbers above 45 inclusive, I would like to add 1; and for numbers less than it, 5.

对于大于等于 45 的数字,我想加 1;对于小于它的数字,5。

I tried

我试过

[x+1 for x in l if x >= 45 else x+5]

But it gives me a syntax error. How can I achieve an ifelselike this in a list comprehension?

但它给了我一个语法错误。我怎样才能在列表理解中实现if-else这样的?

采纳答案by user225312

>>> l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
>>> [x+1 if x >= 45 else x+5 for x in l]
[27, 18, 46, 51, 99, 70, 48, 49, 6]

Do-something if <condition>, else do-something else.

如果做某事<condition>,否则做其他事。

回答by AndiDog

You must put the expression at the beginning of the list comprehension, an if statement at the end filters elements!

您必须将表达式放在列表推导式的开头,最后的 if 语句过滤元素!

[x+1 if x >= 45 else x+5 for x in l]

回答by Dan D.

[x+1 if x >= 45 else x+5 for x in l]

And for a reward, here is the comment, I wrote to remember this the first time I did this error:

为了奖励,这是我第一次犯这个错误时写的评论:

Python's conditional expression is a if C else band can't be used as:

[a for i in items if C else b]

The right form is:

[a if C else b for i in items]

Even though there is a valid form:

[a for i in items if C]

But that isn't the same as that is how you filter by C, but they can be combined:

[a if tC else b for i in items if fC]

Python 的条件表达式是a if C else b并且不能用作:

[a for i in items if C else b]

正确的形式是:

[a if C else b for i in items]

即使有一个有效的形式:

[a for i in items if C]

但这与您过滤的方式不同C,但它们可以组合:

[a if tC else b for i in items if fC]

回答by Jeet

You could move the conditional to:

您可以将条件移动到:

v = [22, 13, 45, 50, 98, 69, 43, 44, 1]
[ (x+1 if x >=45 else x+5)  for x in v ]

But it's starting to look a little ugly, so you might be better off using a normal loop. Note that I used vinstead of lfor the list variable to reduce confusion with the number 1 (I think land Oshould be avoided as variable names under any circumstances, even in quick-and-dirty example code).

但它开始看起来有点难看,所以你最好使用普通循环。请注意,我使用v而不是l作为列表变量来减少与数字 1 的混淆(我认为l并且O在任何情况下都应该避免将其作为变量名,即使在快速而肮脏的示例代码中)。

回答by arboc7

The reason you're getting this error has to do with how the list comprehension is performed.

您收到此错误的原因与列表理解的执行方式有关。

Keep in mind the following:

请记住以下几点:

[ expression for item in list if conditional ]

Is equivalent to:

相当于:

for item in list:
    if conditional:
        expression

Where the expressionis in a slightly different format (think switching the subject and verb order in a sentence).

其中 the 的expression格式略有不同(想想在句子中切换主语和动词顺序)。

Therefore, your code [x+1 for x in l if x >= 45]does this:

因此,您的代码[x+1 for x in l if x >= 45]执行以下操作:

for x in l:
    if x >= 45:
        x+1

However, this code [x+1 if x >= 45 else x+5 for x in l]does this (after rearranging the expression):

但是,此代码[x+1 if x >= 45 else x+5 for x in l]执行此操作(重新排列 后expression):

for x in l:
    if x>=45: x+1
    else: x+5

回答by Stefan Gruenwald

You can also put the conditional expression in brackets inside the list comprehension:

您还可以将条件表达式放在列表推导式中的括号中:

    l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
    print [[x+5,x+1][x >= 45] for x in l]

[false,true][condition] is the syntax

[false,true][condition] 是语法

回答by szeitlin

I just had a similar problem, and found this question and the answers really useful. Here's the part I was confused about. I'm writing it explicitly because no one actually stated it simply in English:

我刚刚遇到了类似的问题,发现这个问题和答案非常有用。这是我感到困惑的部分。我写得很明确,因为实际上没有人用英语简单地表达过:

The iteration goes at the end.

迭代进行到最后。

Normally, a loop goes

通常,一个循环

for this many times:
    if conditional: 
        do this thing
    else:
        do something else  

Everyone states the list comprehension part simply as the first answer did,

每个人都像第一个答案一样简单地陈述了列表理解部分,

[ expression for item in list if conditional ] 

but that's actually not what you do in this case. (I was trying to do it that way)

但这实际上不是你在这种情况下所做的。(我试图这样做)

In this case, it's more like this:

在这种情况下,它更像是这样:

[ expression if conditional else other thing for this many times ] 

回答by Xiaojun Chen

Like in [a if condition1 else b for i in list1 if condition2], the two ifs with condition1and condition2doing two different things. The part (a if condition1 else b)is from a lambda expression:

就像在[a if condition1 else b for i in list1 if condition2],两个ifscondition1condition2做两件不同的事情。该部分(a if condition1 else b)来自 lambda 表达式:

lambda x: a if condition1 else b

while the other condition2is another lambda:

而另一个condition2是另一个 lambda:

lambda x: condition2

Whole list comprehension can be regard as combination of mapand filter:

整个列表可以看作组合mapfilter

map(lambda x: a if condition1 else b, filter(lambda x: condition2, list1))