float('inf') # Set an infinite maximum
float('-inf') # Set an infinite minimum
Commonly used The comparison .
for example , Calculate the cost of path routing when traversing the tree .
Found in the list of options “ The cost is the lowest ” The path of :
lowest_path_cost = float('inf') # Set a maximum
path_costs = [1, 100, 2000000000000, 50]
for path in path_costs:
if path < lowest_path_cost:
lowest_path_cost = path
lowest_path_cost # Find the value with the lowest path cost
1
If you don't float(‘Inf’) You can use , What value would you use as the initial lowest_path_cost?
Of course, it is also possible to set some values randomly ,float(‘Inf’) Eliminate this uncertainty .
contrary float(‘-Inf’) It's the same. There are no examples here .