问题:清单理解条件中的`elif`

我们可以elif在列表理解中使用吗?

范例:

l = [1, 2, 3, 4, 5]

for values in l:
    if values==1:
        print 'yes'
    elif values==2:
        print 'no'
    else:
        print 'idle'

我们可以elif采用与上面的代码类似的方式将列表理解包括在内吗?

例如,答案如下:

['yes', 'no', 'idle', 'idle', 'idle']

到现在为止,我仅使用ifelse理解列表。

Can we use elif in list comprehension?

Example :

l = [1, 2, 3, 4, 5]

for values in l:
    if values==1:
        print 'yes'
    elif values==2:
        print 'no'
    else:
        print 'idle'

Can we include the elif in our list comprehension, in a similar fashion to the code above?

For example, an answer like:

['yes', 'no', 'idle', 'idle', 'idle']

Up until now, I have only used if and else in list comprehension.


回答 0

Python的条件表达式正是针对这种用例而设计的:

>>> l = [1, 2, 3, 4, 5]
>>> ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]
['yes', 'no', 'idle', 'idle', 'idle']

希望这可以帮助 :-)

Python’s conditional expressions were designed exactly for this sort of use-case:

>>> l = [1, 2, 3, 4, 5]
>>> ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]
['yes', 'no', 'idle', 'idle', 'idle']

回答 1

>>> d = {1: 'yes', 2: 'no'}
>>> [d.get(x, 'idle') for x in l]
['yes', 'no', 'idle', 'idle', 'idle']
>>> d = {1: 'yes', 2: 'no'}
>>> [d.get(x, 'idle') for x in l]
['yes', 'no', 'idle', 'idle', 'idle']

回答 2

可以的。

请注意,当您使用以下语法时:

['yes' if v == 1 else 'no' for v in l]

您正在使用if / else运算符的三进制形式(如果您熟悉C之类的语言,则类似于?:Construct:)(v == 1 ? 'yes' : 'no')

if / else运算符的三进制形式没有内置的“ elif”,但是您可以在“ else”条件下对其进行仿真:

['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]

这就像在说:

for v in l:
    if v == 1 :
        print 'yes'
    else:
        if v == 2:
            print 'no'
        else:
            print 'idle'

因此,没有像您问的那样直接的“ elif”构造,但是可以使用嵌套的if / else语句来模拟它。

You can, sort of.

Note that when you use sytax like:

['yes' if v == 1 else 'no' for v in l]

You are using the ternary form of the if/else operator (if you’re familiar with languages like C, this is like the ?: construct: (v == 1 ? 'yes' : 'no')).

The ternary form of the if/else operator doesn’t have an ‘elif’ built in, but you can simulate it in the ‘else’ condition:

['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]

This is like saying:

for v in l:
    if v == 1 :
        print 'yes'
    else:
        if v == 2:
            print 'no'
        else:
            print 'idle'

So there’s no direct ‘elif’ construct like you asked about, but it can be simulated with nested if/else statements.


回答 3

也许您想要这样:

l = [1, 2, 3, 4, 5] 

print ([['idle','no','yes'][2*(n==1)+(n==2)] for n in l])

Maybe you want this:

l = [1, 2, 3, 4, 5] 

print ([['idle','no','yes'][2*(n==1)+(n==2)] for n in l])

回答 4

您可以使用列表理解功能,以便从原始列表创建另一个列表。

>>> l = [1, 2, 3, 4, 5]
>>> result_map = {1: 'yes', 2: 'no'}
>>> [result_map[x] if x in result_map else 'idle' for x in l]
['yes', 'no', 'idle', 'idle', 'idle']

You can use list comprehension is you are going to create another list from original.

>>> l = [1, 2, 3, 4, 5]
>>> result_map = {1: 'yes', 2: 'no'}
>>> [result_map[x] if x in result_map else 'idle' for x in l]
['yes', 'no', 'idle', 'idle', 'idle']

回答 5

另一种简单的方法是使用条件列表理解,如下所示:

l=[1,2,3,4,5]
print [[["no","yes"][v==1],"idle"][v!=1 and v!=2] for v in l]

给您正确的答案:

[‘是’,’否’,’空闲’,’空闲’,’空闲’]

Another easy way is to use conditional list comprehension like this:

l=[1,2,3,4,5]
print [[["no","yes"][v==1],"idle"][v!=1 and v!=2] for v in l]

gives you the correct anwer:

[‘yes’, ‘no’, ‘idle’, ‘idle’, ‘idle’]


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。