我似乎找不到可以告訴我以下內(nèi)容的有效來源,我正在嘗試了解不同的 Python 技術(shù),誰能解釋這些代碼行,并可能顯示它們的等效內(nèi)容?shortest_path = {initial: (None, 0)}next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}current_node = min(next_destinations, key=lambda k: next_destinations[k][1])作為參考,一個節(jié)點是一個字符串。
1 回答

躍然一笑
TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊
1號線
shortest_path = {initial: (None, 0)}
創(chuàng)建了字典。1 鍵:initial
。它的值:(None, 0)
這是一個帶有None
和0
(零)的元組。
2號線
next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}
字典理解。映射node
到shortest_paths[node]
. 它for
每個都node in shortest_paths
做,并且只做if node not in visited
這一行的輸出是一個字典
3號線
current_node = min(next_destinations, key=lambda k: next_destinations[k][1])
嘗試找到最小值并將其分配給current_node
。敏什么?上next_destinations
。如何知道如何選擇?通過key
給出的函數(shù)k
,檢查next_destinations[k][1]
添加回答
舉報
0/150
提交
取消