简体   繁体   English

如何在一行中编写一个for循环和多个if语句?

[英]How to write a for loop and multiple if statements in one line?

Is there a way to write something like this in one line?有没有办法在一行中写这样的东西?

for x in list: 
   if condition1: 
       (...) 
   elif condition2: 
       (...) 
   else: 
       (...) 

Another way of asking is: Is it possible to combine following list comprehensions?另一种提问方式是:是否可以结合以下列表推导式?

(...) for x in list

and

123 if condition1 else 345 if condition2 else 0

What you want to do would almost certainly be considered bad style.你想做的事情几乎肯定会被认为是糟糕的风格。 Maybe it's an XY problem ?也许这是一个XY问题 In that case, you should open another question with the underlying issue.在这种情况下,您应该针对潜在问题提出另一个问题。

If you're sure this is what you want, have a look at the following example, using PEP 308 -- Conditional Expressions :如果您确定这是您想要的,请查看以下示例,使用PEP 308 -- Conditional Expressions

>>> def f(condition1, condition2):
...     return 1 if condition1 else 2 if condition2 else 3
... 
>>> f(True, False)
1
>>> f(True, True)
1
>>> f(False, True)
2
>>> f(False, False)
3

Subsequently, your example随后,你的例子

for x in list: 
   if condition1: 
       (...) 
   elif condition2: 
       (...) 
   else: 
       (...) 

could be written as a list comprehension as follows:可以写成列表理解如下:

[(...) if condition1 else (...) if condition2 else (...) for x in list]

use lsit comprehensions使用 lsit 理解

var = [i for i in list if i == something or i == something] more on that here var = [i for i in list if i == something or i == something] 更多关于这里

Yes, there are ways, but not recommended.是的,有办法,但不推荐。 Here is an example of how you could do it:这是一个如何做到的示例:

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

for x in l:
  if x%2 == 0:
    print("Even")
  elif x%2 == 1:
    print("Odd")
  else:
    print("Nothing")

print("And here the one-liner:")

[print("Even") if x%2 == 0 else print("Odd") if x%2 == 1 else print("Nothing") for x in l]

I don't recommend this way, because of readability.我不推荐这种方式,因为可读性。 Read The Zen of Python , don't make too long lines (max 80 characters)阅读Python 之禅,不要写太长的行(最多 80 个字符)

The way to write for loop in a single line , mostly used in Data Science Project, You can use this way, as we have six labeled fake news LIAR:在单行线,主要是在数据科学项目中使用循环写入的方式,您可以使用此方法,因为我们有六个标假新闻LIAR:

Labels: ['barely-true' 'false' 'half-true' 'mostly-true' 'pants-fire' 'true'], to represent this as a binary labels:标签:['barely-true' 'false' 'half-true' 'mostly-true' 'pants-fire' 'true'],将其表示为二进制标签:

We use the following way:我们使用以下方式:

labels = [ 1 if lab=='false' or lab=='pants-fire' or lab=='barely_true'  else 0 for lab in df.is_fake]

Another way, the same if-else condition for loop :另一种方式,同样的 if-else 条件for loop

labels = [ 1 if lab=='false' else 1 if lab=='pants-fire' else 1 if lab=='barely_true'  else 0 if lab == 'true' else 0 if lab == 'half-true' else 0 for lab in df.is_rumor]

Hope to help many of you, who want to do the same way in many problem-solving.希望能帮助到很多想用同样方法解决问题的人。

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

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