我似乎找不到可以告诉我以下内容的有效来源,我正在尝试了解不同的 Python 技术,谁能解释这些代码行,并可能显示它们的等效内容?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])作为参考,一个节点是一个字符串。
1 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
1号线
shortest_path = {initial: (None, 0)}创建了字典。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给出的函数k,检查next_destinations[k][1]
添加回答
举报
0/150
提交
取消
