mercredi 27 juillet 2016

Saving a key from a dictionary without value

I am representing a weighted graph as a dictionary where the key represents a vertex and the following lists, the links that are connecting the vertex (first number weight and second number linked vertex): wGraph = { 1 : [[2, 2],[3, 4],[3, 3]], 3 : [[3, 2],[5, 4],[7, 5]], 2 : [[2, 1],[4, 4],[3, 6]], 4 : [[3, 1],[4, 2],[1, 6],[5, 3]], 6 : [[3, 2],[1, 4],[8, 5]], 5 : [[7, 3],[8, 6],[9, 7]], 7 : [9, 5]} I would like to save a random key without its value from the dictionary to a list called visited. random_num = random.randrange(len(wGraph)) visited = [] How can I get the key from the dictionary according to the random number?

Aucun commentaire:

Enregistrer un commentaire