[英]Pythonic way to find key of weighted minimum and maximum from a dictionary
[英]Pythonic way to find a minimum dictionary key with value in a list?
我正在 Python 中編寫 Astar 算法的實現,在某些時候我想要 select open_set
中具有最低f_score value
的元素:
# current is the node in open_set having the lowest f_score value
current = min_f_score(open_set, f_score)
open_set
是一個元組(x, y)
的列表,表示一個點在 2D 中的坐標, f_score
是一個字典{(x, y): score}
,它為每個元組(x, y)
分配其f_score
值。
我對min_f_score
的第一個實現如下:
def min_f_score(open_set, f_score):
# First we initialize min_score_element and min_score
min_score_element = open_set[0]
min_score = f_score[open_set[0]]
# Then we look for the element from f_score keys with the lowest value
for element in open_set:
if element in f_score.keys() and f_score[element] < min_score:
min_score = f_score[element]
min_score_element = element
return min_score_element
它工作正常,但我想知道我是否可以想出一些更精簡、更 pythonic 的代碼。 經過一些研究,我想出了另外兩個實現:
def min_f_score(open_set, f_score):
# We filter the elements from open_set and then find the min f_score value
return min(filter(lambda k: k in open_set, f_score), key=(lambda k: f_score[k]))
和:
def min_f_score(open_set, f_score):
# We look for the min while assigning inf value to elements not in open_set
return min(f_score, key=(lambda k: f_score[k] if k in open_set else float("inf")))
兩者似乎都有效並給我相同的結果,但比第一個實施慢得多。
出於好奇,我想知道是否有更好的方法來實現min_f_score
?
編輯:按照@azro(謝謝)的建議,我添加了一個要執行的代碼示例:
open_set = [(1, 2), (1, 3), (2, 1), (2, 2), (3, 1), (1, 4), (4, 1), (1, 5), (5, 0), (5, 1), (0, 6), (1, 6)]
f_score = {(0, 0): 486.0, (0, 1): 308.0, (1, 0): 308.0, (1, 1): 265.0, (0, 2): 265.0, (1, 2): 338.0, (0, 3): 284.0, (1, 3): 450.0, (2, 0): 265.0, (2, 1): 338.0, (2, 2): 629.0, (3, 0): 284.0, (3, 1): 450.0, (0, 4): 310.0, (1, 4): 550.0, (4, 0): 310.0, (4, 1): 564.0, (0, 5): 316.0, (1, 5): 588.0, (5, 0): 316.0, (5, 1): 606.0, (0, 6): 298.0, (1, 6): 534.0}
min_f_score(open_set, f_score)
Output: (0, 6)
帶有dict.get()
的版本比你的 2 min()
版本快得多(慢了 10 倍以上),但仍然慢了大約 2 倍
def min_f_score_d(open_set, f_score):
inf = float("inf")
return min(open_set, key=lambda k: f_score.get(k, inf))
@stefan-pochmann建議,那個比經典迭代慢一點
def min_f_score(open_set, f_score):
return min(f_score.keys() & open_set, key=f_score.get)
if element in f_score and f_score[element] < min_score:
# if faster than
if element in f_score.keys() and f_score[element] < min_score:
from timeit import timeit
A = """
def min_f_score(open_set, f_score):
min_score_element, min_score = open_set[0], f_score[open_set[0]]
for element in open_set:
if element in f_score and f_score[element] < min_score:
min_score = f_score[element]
min_score_element = element
return min_score_element
"""
B = """
def min_f_score(open_set, f_score):
inf = float("inf")
return min(open_set, key=lambda k: f_score.get(k, inf))
"""
C = """
def min_f_score(open_set, f_score):
return min(f_score.keys() & open_set, key=f_score.get)
"""
SETUP = """
from random import randrange
open_set = [(randrange(1000), randrange(1000)) for _ in range(1000)]
f_score = {pair: randrange(1000) for pair in open_set[:len(open_set) // 2]}
"""
NB = 20_000
print(timeit(setup=SETUP + A, stmt="min_f_score(open_set, f_score)", number=NB)) # ~2.7
print(timeit(setup=SETUP + B, stmt="min_f_score(open_set, f_score)", number=NB)) # ~4.8
print(timeit(setup=SETUP + C, stmt="min_f_score(open_set, f_score)", number=NB)) # ~3.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.