如何以pythonic方式做到这一点?

时间:2022-02-02 22:41:55

Consider this Python snippet:

考虑这个Python片段:

for a in range(10):

    if a == 7:
        pass
    if a == 8:
        pass
    if a == 9:
        pass
    else:
        print "yes"

How can it be written shorter?

怎么写得更短?

#Like this or...
if a ?????[7,8,9]:
    pass

8 个解决方案

#1


17  

Use the in operator:

使用in运算符:

if a in (7,8,9):
    pass

#2


15  

To test if a falls within a range:

要测试a是否属于某个范围:

if 7 <= a <= 9:
  pass

To test if a is in a given sequence:

要测试a是否在给定的序列中:

if a in [3, 5, 42]:
  pass

#3


2  

for a in range(10):
    if a > 6:
        continue
    print('yes')

#4


2  

Based on your original code the direct "pythonic" replacement is:

根据您的原始代码,直接“pythonic”替换是:

if not a in [7, 8, 9]:
     print 'yes'

or

要么

if a not in [7, 8, 9]:
     print 'yes'

The latter reads a little better, so I guess it's a bit more "pythonic".

后者读得好一点,所以我猜它有点“pythonic”。

#5


1  

if a in [7,8,9]

#6


1  

Depending on what you want to do, the map() function can also be interesting:

根据你想要做的事情,map()函数也很有趣:

def _print(x):
    print 'yes'

map(_print, [a for a in range(10) if a not in (7,8,9)])

#7


1  

What about using lambda.

怎么样使用lambda。

>>> f = lambda x: x not in (7, 8, 9) and print('yes')
>>> f(3)
yes
>>> f(7)
False

#8


1  

Since the question is tagged as beginner, I'm going to add some basic if-statement advice:

由于问题被标记为初学者,我将添加一些基本的if语句建议:

if a == 7:
    pass
if a == 8:
    pass
if a == 9:
   ...
else:
   ...

are three independent if statements and the first two have no effect, the else refers only to

是三个独立的if语句,前两个没有效果,else仅指

 if a == 9:

so if a is 7 or 8, the program prints "yes". For future use of if-else statement like this, make sure to use elif:

因此,如果a为7或8,程序将打印“是”。为了将来使用这样的if-else语句,请确保使用elif:

if a == 7:
    seven()
elif a == 8:
    eight()
elif a == 9:
    nine()
else:
    print "yes"

or use just one if-statement if they call for the same action:

或者只使用一个if语句,如果他们要求相同的操作:

if a == 7 or a == 8 or a == 9:
    seven_eight_or_nine()
else:
    print "yes"

#1


17  

Use the in operator:

使用in运算符:

if a in (7,8,9):
    pass

#2


15  

To test if a falls within a range:

要测试a是否属于某个范围:

if 7 <= a <= 9:
  pass

To test if a is in a given sequence:

要测试a是否在给定的序列中:

if a in [3, 5, 42]:
  pass

#3


2  

for a in range(10):
    if a > 6:
        continue
    print('yes')

#4


2  

Based on your original code the direct "pythonic" replacement is:

根据您的原始代码,直接“pythonic”替换是:

if not a in [7, 8, 9]:
     print 'yes'

or

要么

if a not in [7, 8, 9]:
     print 'yes'

The latter reads a little better, so I guess it's a bit more "pythonic".

后者读得好一点,所以我猜它有点“pythonic”。

#5


1  

if a in [7,8,9]

#6


1  

Depending on what you want to do, the map() function can also be interesting:

根据你想要做的事情,map()函数也很有趣:

def _print(x):
    print 'yes'

map(_print, [a for a in range(10) if a not in (7,8,9)])

#7


1  

What about using lambda.

怎么样使用lambda。

>>> f = lambda x: x not in (7, 8, 9) and print('yes')
>>> f(3)
yes
>>> f(7)
False

#8


1  

Since the question is tagged as beginner, I'm going to add some basic if-statement advice:

由于问题被标记为初学者,我将添加一些基本的if语句建议:

if a == 7:
    pass
if a == 8:
    pass
if a == 9:
   ...
else:
   ...

are three independent if statements and the first two have no effect, the else refers only to

是三个独立的if语句,前两个没有效果,else仅指

 if a == 9:

so if a is 7 or 8, the program prints "yes". For future use of if-else statement like this, make sure to use elif:

因此,如果a为7或8,程序将打印“是”。为了将来使用这样的if-else语句,请确保使用elif:

if a == 7:
    seven()
elif a == 8:
    eight()
elif a == 9:
    nine()
else:
    print "yes"

or use just one if-statement if they call for the same action:

或者只使用一个if语句,如果他们要求相同的操作:

if a == 7 or a == 8 or a == 9:
    seven_eight_or_nine()
else:
    print "yes"