将if条件放在列表推导中的位置? [英] Where to put the if-condition inside a list comprehension?

查看:105
本文介绍了将if条件放在列表推导中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被赋予了功能:

x**2 - 4*x + 6

,任务是找到0到10之间的最小整数x. 我不得不使用for循环:

and the task was to find the minimum integer values x between 0 and 10. I had to use a for loop:

for i in range(11):
    if 2*i-4==0:
        print("Minimum of the given function is:", i)

这给了我正确答案2.

现在我应该将其翻译为列表理解,但我不知道在何处放置if语句:

Now I should translate that as a list comprehension, but I do not know where to put the if statement:

mylist = [2*i-4==0 for i in range(11)]
print(mylist)

当然,作为输出,我得到一个true/false列表,其中索引2是正确的true.但是,如何将if纳入列表理解范围?

Of course, as an output I get a true/false list, with the index 2 being the correct true. But How do I include the if in my list comprehension?

推荐答案

您正在找到一个单个值,而不是值列表.列表理解在这里毫无意义.

You are finding a single value, not a list of values. A list comprehension makes little sense here.

您的错误是将测试放入列表理解的值表达式部分.那不是一个过滤器,而是从循环中(已过滤的)元素构建新列表值的部分.

Your error is in putting your test in the value expression section of the list comprehension. That's not a filter, that's the part where you build the new list values from the (filtered) elements from the loop.

for循环部分之后放置if :

Put an if after the for loop part:

[i for i in range(11) if 2 * i - 4 == 0]

这将建立一个仅包含一个元素的列表,该元素的i值为真:

This builds a list with just one element, the i value for which the value is true:

>>> [i for i in range(11) if 2 * i - 4 == 0]
[2]

如果必须单行执行此操作,请使用 生成器表达式 ,然后使用 函数从中获得第一个结果:

If you must do this in a single line, use a generator expression, then use the next() function to get the first result from that:

gen = (i for i in range(11) if 2 * i - 4 == 0)
result = next(gen)

即使在if测试为true的i可能有多个值的情况下,这也会为您提供i的最小值.

This will get you the minimal value for i even if there were to be multiple possible values for i where the if test would be true.

生成器被懒惰地评估;循环在i达到if测试为真的值时停止,并产生一个值. next()仅返回该第一个值,因此不再进行迭代.就像您在for循环中使用了break(除非以后可以继续循环).

A generator is lazily evaluated; the loop stops the moment i reaches a value where the if test is true, and that one value is produced. next() only returns that first value, so no more iteration takes place. It's as if you used break in a for loop (except you can continue the loop later).

请注意,如果生成器不产生任何任何值,则这可能引发StopIteration异常.在这种情况下,您可以告诉next()返回默认值.如果否则将引发StopIteration,则返回next()的第二个参数:

Note that this can raise a StopIteration exception if the generator doesn't produce any values. You can tell next() to return a default value in that case; a second argument to next() is returned if StopIteration would be raised otherwise:

result = next(gen, None)

您可以在next()调用中组合生成器表达式;如果生成器表达式是唯一的参数,则可以省略生成器表达式的(...)括号,否则将其包括在内,以便可以将其与其他参数区分开来

You can combine the generator expression in the next() call; if a generator expression is the only argument you can omit the (...) parenthesis of the generator expression, otherwise include them so that it can be distinguished from other arguments:

result = next((i for i in range(11) if 2 * i - 4 == 0), None)

这篇关于将if条件放在列表推导中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆