Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less than Or Greater Than Comparison as a Variable in Python

I have a block of code in a function that does some comparisons, namely:

if customer_info['total_dls'] < min_training_actions \
or customer_info['percentage'] > train_perc_cutoff:
    continue
elif customer_id not in test_set \
or test_set[customer_id]['total_dls'] < min_testing_actions:
    num_uncertain +=1
    continue
elif test_set[customer_id]['percentage'] <= test_perc_cutoff:
    num_correct +=1
else:
    num_incorrect +=1

Now sometimes I need to do those comparisons to be greater than, and sometimes I need them to be less than. All of the rest of the code is exactly the same. Now, I could just make two functions that reuse basically the same code, but before I do, is there some clean way to variabalize the comparison operator, so I could just use the same block of code and pass in the comparison as a variable? Something like: compare(var1, var2, polarity). I know I can make this myself, but I'm wondering what the standard is in cases like this. Is there some pretty pythonic way of doing this I'm unaware of?

[Edit] Adding emphasis to the most important part of the question [/Edit]

like image 529
Eli Avatar asked Apr 28 '14 18:04

Eli


People also ask

How do you compare values to a variable in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=

How do you check if a number is greater or less than in Python?

How do you check if a number is greater than another in Python? Use the > operator to check if a value is greater than another value. Insert the greater-than operator > in between two values to create a boolean expression that returns True if the left value is greater than the right value and False if otherwise.


2 Answers

You can use the operator module; it has functions that act as the comparison operators:

import operator

if foobar:
    comp = operator.lt
else:
    comp = operator.gt

if comp(spam, eggs):

This'll use either test if spam is less then, or greater then eggs, depending on the truth of foobar.

This fits your comparison as a variable requirement exactly.

This is certainly what I'd use to avoid repeating myself; if you have two pieces of code that differ only in the comparison direction, use a function from operators to parameterize the comparison.

like image 172
Martijn Pieters Avatar answered Nov 15 '22 20:11

Martijn Pieters


I had the same question when trying to build a module for unknown ML models. Some require a minimum score to perform, others a maximum score, depending on the metric used. I passed in a 'greater than' boolean variable and wrote a small function:

def is_better(gt,a,b): 
    return a>b if gt else a<b
like image 29
Dany Majard Avatar answered Nov 15 '22 20:11

Dany Majard