简体   繁体   English

如果一个值等于值列表中的一个,是否有一种方法可以让 If 语句返回真 Python

[英]Is There a Way for an If Statement to Return True if a Value is Equal to One of a List of Values Python

Is there a way to do something like this in python, where if a value is equal to one of a set of values, the if statement returns true (without using or and then replicating the first expression and the condition)?有没有办法在 python 中做这样的事情,如果一个值等于一组值中的一个,则 if 语句返回 true(不使用or然后复制第一个表达式和条件)?

li1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
li2 = []
for n in li1:
    if n == 2, 3, 5, 8:
        li2.append(n)

This was an example without list comprehension but I want to have something like this with list comprehension, considering the whole point of it is to make code shorter.这是一个没有列表理解的例子,但我想有这样的列表理解,考虑到它的全部意义是让代码更短。

li = [int + 1 for int in range(10) if int == 2, 3, 5, 8]

But I haven't found anything that could do this.但我还没有找到任何可以做到这一点的东西。 I know you can use or and do something like this,我知道你可以使用or做这样的事情,

li = [int + 1 for int in range(10) if int + 1 == 2 or int + 1 == 3 or int + 1 == 5 or int + 1 == 8]

but it just seems lengthy.但它似乎很长。 I've tried a lot of things like using lists, tuples, range() , or (without copying the first expression and the condition), etc. It either just says invalid syntax or doesn't do the right thing.我已经尝试了很多东西,比如使用列表、元组、 range() or (不复制第一个表达式和条件)等。它要么只是说语法无效,要么没有做正确的事情。 I don't really know how to phrase the question, so I can't use a google search.我真的不知道如何表达这个问题,所以我不能使用谷歌搜索。

If anyone knows whether or not this can be done, thank you in advance: :)如果有人知道这是否可以完成,请先谢谢您::)

There are multiple ways to do that.有多种方法可以做到这一点。
One would be to use if int in [2, 3, 5, 8] .一种是使用if int in [2, 3, 5, 8] Basically this: li = [int + 1 for int in range(10) if int in [2, 3, 5, 8]]基本上是这样的: li = [int + 1 for int in range(10) if int in [2, 3, 5, 8]]

Or of course just this: li = [int + 1 for int in [2, 3, 5, 8]]或者当然只是这样: li = [int + 1 for int in [2, 3, 5, 8]]

You can also use lambdas, if you want to have a more complex condition:如果你想要更复杂的条件,你也可以使用 lambda:
li = [int + 1 for int in range(10) if (lambda x: x % 3 == 0)(int)]
You would not need a lambda for that, but this tests if your int is a multiple of three.为此,您不需要 lambda,但这会测试您的int是否是三的倍数。

Some thought about lambdas: Here the lambda does not really make sense as you could just write int % 3 == 0 without the lambda. I don't want to go into detail about lambdas, as this would be out of scope. So if you want some great examples on how/when to use them, have a look at this answer .一些关于 lambda 的想法:这里的 lambda 并没有真正意义,因为你可以只写int % 3 == 0而没有 lambda。我不想 go 详细介绍 lambda,因为这将超出 scope。所以如果你想要一些关于如何/何时使用它们的很好的例子,看看这个答案

This if n == 2, 3, 5, 8 on the other hand is not valid python.另一方面if n == 2, 3, 5, 8 2、3、5、8 则无效 python。

Additional:额外的:
int is a built-in which should not be used as a variable name. int是内置的,不应用作变量名。 Even if it is syntactically correct.即使它在语法上是正确的。

li = [int + 1 for int in range(10) if int + 1 in [2, 3, 5, 8]]

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

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