相关疑难解决方法(0)

Python反向/反转映射

给出这样的字典:

my_map = {'a': 1, 'b': 2}
Run Code Online (Sandbox Code Playgroud)

如何反转此地图以获得:

inv_map = {1: 'a', 2: 'b'}
Run Code Online (Sandbox Code Playgroud)

编者注: __CODE__改为__CODE__避免与内置函数冲突,__CODE__.下面有些评论可能会受到影响.

python mapping reverse dictionary

597
推荐指数
12
解决办法
36万
查看次数

如何更正错误'AttributeError:'dict_keys'对象没有属性'remove''?

我正在尝试使用dijkstra算法进行最短路径查找,但似乎无法正常工作.无法弄清问题是什么.这是代码和错误消息.(我正在研究Python 3.5.https ://www.youtube.com/watch?v=LHCVNtxb4ss)

graph = {
    'A': {'B': 10, 'D': 4, 'F': 10},
    'B': {'E': 5, 'J': 10, 'I': 17},
    'C': {'A': 4, 'D': 10, 'E': 16},
    'D': {'F': 12, 'G': 21},
    'E': {'G': 4},
    'F': {'E': 3},
    'G': {'J': 3},
    'H': {'G': 3, 'J': 3},
    'I': {},
    'J': {'I': 8},
}

def dijkstra(graph, start, end):
    D = {}
    P = {}
    for node in graph.keys():
        D[node]= -1
        P[node]=""
    D[start]=0
    unseen_nodes=graph.keys()
    while len(unseen_nodes) > 0:
        shortest=None
        node=' '
        for …
Run Code Online (Sandbox Code Playgroud)

python dictionary graph-algorithm python-3.x python-3.5

9
推荐指数
1
解决办法
1万
查看次数