Home > Software engineering >  Is there a concise way to program these two conditions?
Is there a concise way to program these two conditions?

Time:09-15

exactSearch is a boolean variable. True means looking for an exact match with ==, False means looking for a like match with 'in'.

The resulting operations of appending to a DataFrame or continuing are the same regardless of the value of exactSearch, just that the operator will change depending on exactSearch.

The following code does indeed work, but I'm curious if there's a better way to program these conditions concisely.

if exactSearch:
    if payerName == payer: # Look for exact match
        result.loc[len(result)] = [provider,payer]   status.split(" ") # If located, add to results
    else:
        continue
        
else:
    if payerName in payer: # Look for like match
        result.loc[len(result)] = [provider,payer]   status.split(" ") # If located, add to results
    else:
        continue

CodePudding user response:

How aboout:

match = (payerName == payer) if exactSearch else (payerName in payer)
if match:
    result.loc[len(result)] = [provider,payer]   status.split(" ") # If located, add to results
else:
    continue

CodePudding user response:

Would this work?

if (payerName == payer) or (payerName in payer):
    result.loc[len(result)] = [provider,payer]   status.split(" ") 
else:
    continue
  • Related