您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中float('inf')的意义是什么?

Python中float('inf')的意义是什么?

它用作比较的无上限上限值。这对于查找某物的最低值很有用。例如,计算遍历树木时的路径路线成本。

例如,在选项列表中找到“最便宜”的路径:

>>> lowest_path_cost = float('inf')
>>> # pretend that these were calculated using some worthwhile algorithm
>>> path_costs = [1, 100, 2000000000000, 50]
>>> for path in path_costs:
...   if path < lowest_path_cost:
...     lowest_path_cost = path
...
>>> lowest_path_cost
1

如果您没有float('Inf')空余时间,您将使用什么价值the initial lowest_path_cost?就9999999足够-float('Inf')消除了这个猜测。

python 2022/1/1 18:32:58 有213人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶