Pythonic way to replace > with < in the midst of a big for loop

DRJ

I'm trying to write a big for-loop that executes some nested logic. I'd like to control which comparisons get applied with a kwarg in my function. Basically, I have two different comparisons, both controlled by kwargs in my function. Currently, in order to make sure that either could be a minimization, or a maximization, I have to copy/paste my for-loop for times. This feels clunky.

def calc_best_points(observations, min_cost=False, min_value=False):
  for point in observations:
    blah blah blah
      if cost > best_cost:
        if value > best_value:
          other logic here
          res.append(point)
          best_cost, best_value = cost, value

Basically, I'd like to write something like:

if min_cost: comparison_to_apply = <

and then futher down:

if cost comparison_to_apply best_cost:

Is there a way to do that, or do I just need to copy/paste my for-loop four times, based on the various combinations of comparisons I might want to do?

Barmar

You can use the operator module to put the operator function in a variable.

import operator

def calc_best_points(observations, min_cost=False, min_value=False):
    cost_compare = operator.lt if min_cost else operator.gt
    value_compare = operator.lt if min_value else operator.gt

    for point in observations:
        # blah blah blah
        if cost_compare(cost, best_cost):
            if value_compare(value, best_value):
                other logic here
                res.append(point)
                best_cost, best_value = cost, value

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pythonic way to replace chars

From Java

Pythonic way to combine FOR loop and IF statement

From Dev

Pythonic way to default loop counters

From Dev

Pythonic way to loop over dictionary

From Dev

Pythonic way to break out of loop

From Dev

What is the pythonic way to to resume a loop within a loop

From Dev

pythonic way to replace two character in a string

From Dev

Pythonic way to find and replace with a dictionary of lists

From Python

Pythonic way to replace values in a list according to a dictionary

From Dev

how to change for loop code to pythonic way

From Dev

Pythonic way to make dictionary within a for loop

From Dev

Pythonic way to detect final iteration of a loop

From Dev

Pythonic way of handling two statements in a for loop?

From Java

What is the pythonic way to detect the last element in a 'for' loop?

From Dev

Pythonic way of executing a loop on a dictionary of lists

From Dev

Pythonic way to join a string in a loop to create textblock

From Dev

Is there a more pythonic way of writing a loop that modifies a list?

From Dev

Is there a pythonic way for loop and use previous item

From Dev

Pythonic way to create a big random base-29 number

From Dev

How to replace every second space with a comma the Pythonic way

From Dev

Pythonic way to replace every second comma of string with space

From Dev

pythonic way to replace special char at beginning or at the end of string

From Dev

python replace substrings in a string if substring in list // pythonic way

From Dev

Pythonic way of replace values in one column from a two column table

From Dev

Is there a better way to replace the "for" loop in python?

From Dev

Is there are more pythonic way to write a while loop that only updates a variable?

From Dev

What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

From Dev

How to stop code execution of for loop from try/except in a Pythonic way?

From Dev

What is the pythonic way of running an asyncio event loop forever?

Related Related

  1. 1

    Pythonic way to replace chars

  2. 2

    Pythonic way to combine FOR loop and IF statement

  3. 3

    Pythonic way to default loop counters

  4. 4

    Pythonic way to loop over dictionary

  5. 5

    Pythonic way to break out of loop

  6. 6

    What is the pythonic way to to resume a loop within a loop

  7. 7

    pythonic way to replace two character in a string

  8. 8

    Pythonic way to find and replace with a dictionary of lists

  9. 9

    Pythonic way to replace values in a list according to a dictionary

  10. 10

    how to change for loop code to pythonic way

  11. 11

    Pythonic way to make dictionary within a for loop

  12. 12

    Pythonic way to detect final iteration of a loop

  13. 13

    Pythonic way of handling two statements in a for loop?

  14. 14

    What is the pythonic way to detect the last element in a 'for' loop?

  15. 15

    Pythonic way of executing a loop on a dictionary of lists

  16. 16

    Pythonic way to join a string in a loop to create textblock

  17. 17

    Is there a more pythonic way of writing a loop that modifies a list?

  18. 18

    Is there a pythonic way for loop and use previous item

  19. 19

    Pythonic way to create a big random base-29 number

  20. 20

    How to replace every second space with a comma the Pythonic way

  21. 21

    Pythonic way to replace every second comma of string with space

  22. 22

    pythonic way to replace special char at beginning or at the end of string

  23. 23

    python replace substrings in a string if substring in list // pythonic way

  24. 24

    Pythonic way of replace values in one column from a two column table

  25. 25

    Is there a better way to replace the "for" loop in python?

  26. 26

    Is there are more pythonic way to write a while loop that only updates a variable?

  27. 27

    What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

  28. 28

    How to stop code execution of for loop from try/except in a Pythonic way?

  29. 29

    What is the pythonic way of running an asyncio event loop forever?

HotTag

Archive