'Creating a distance matrix from a matrix/list

I have created a list from the input and from the list I have created a matrix also. list-

('A', 'B', 3)
('A', 'D', 4)
('B', 'D', 4)
('B', 'H', 5)
('C', 'L', 2)
('D', 'F', 1)
('F', 'H', 3)
('G', 'H', 2)
('G', 'Y', 2)
('I', 'J', 6)
('I', 'K', 4)

matrix-

 ['A' 'B' '3']
 ['A' 'D' '4']
 ['B' 'D' '4']
 ['B' 'H' '5']
 ['C' 'L' '2']
 ['D' 'F' '1']
 ['F' 'H' '3']
 ['G' 'H' '2']
 ['G' 'Y' '2']
 ['I' 'J' '6']
 ['I' 'K' '4']

However I want to create a distance matrix from the above matrix or the list and then print the distance matrix. what will be the correct approach to implement it. In the above matrix the first 2 nodes represent the starting and ending node and the third one is the distance. I am ready to give any further clarification if required.A sample distance matrix is -

[[0, 10, 15, 20], 
 [10, 0, 35, 25],
 [15, 35, 0, 30], 
 [20, 25, 30, 0]]


Solution 1:[1]

Since you did not show any attempts, here some ideas to get you started.

  1. Convert a character to a number, e.g. an index location: ord('C') - ord('A')

  2. Creating a matrix: If you want to have a fixed size pre-allocated matrix, instead of lists in lists which have flexible size, the library numpy can help you. Then you can access fields and set values while looping through your data.

  3. If you are really interested in analyzing a network, you could have a look into networkx.

Please take these ideas as a starting point and refine your question with more precise obstacles you encounter while trying to solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carlos Horn