Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
How to remove repetition when there is a common condition

How do i remove the repetitions

 if a < 2 or b < 2 or c < 2 or d < 2: 
       pass #logic

I want to remove the repetitions. Something like this:

if (a,b,c,d) < 2:
   pass #logic 
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I wouldn't import numpy just for this as other answers suggest. It's probably overkill.

I would do something like:

condition = lambda x: x < 2
if any(condition(x) for x in (a, b, c, d)):
    # whatever you want

Or more compact (but less reusable):

if any(x < 2 for x in (a, b, c, d)):
    # whatever you want
over 4 years ago · Santiago Trujillo Relatório

0

Another slightly different way is to check if the smallest variable is less than the target value. In other words, if the smallest variable is not less than the target - then surely none of them is:

if min(a, b, c, d) < 2:
    # do something

This however needs to actually find the minimal value behind the scenes and loses the short-circuiting advantage of using any with a generator expression.

over 4 years ago · Santiago Trujillo Relatório

0

When using numpy this can be done with .all() and .any():

import numpy as np

array = np.asarray([1,2,3])

if (array < 2).any():
    print('True')
else:
    print('False')

#output: True

if (array < 2).all():
    print('True')
else:
    print('False')

#output: False

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda