0

I have seen lots of example of lists into arrays, but no examples of lists in this format into arrays, which is strange because the list format I present is the standard go-to way of defining a graph, point-to-point mapping that you would find in any table, csv, database, etc. I tried everything here with no luck. Thanks for any ideas.

Input and desired output

input= [[A, A, 0],
 [A, B, 5],
 [A, C, 3],
 [B, A, 5],
 [B, B, 0],
 [B, C, 6],
 [C, A, 3],
 [C, B, 6],
 [C, C, 0]]
desiredOutput= [[0, 5, 3],
 [5, 0, 6],
 [3, 6, 0]]
PM 2Ring
55.6k6 gold badges95 silver badges200 bronze badges
asked Oct 5, 2017 at 14:50
2
  • input= [[A, A, 0], [A, B, 5], [A, C, 3], [B, A, 5], [B, B, 0], [B, C, 6], [C, A, 3], [C, B, 6], [C, C, 0]] desiredOutput= [[0, 5, 3], [5, 0, 6], [3, 6, 0]] Commented Oct 5, 2017 at 14:59
  • I think a module like pandas is often the way you can handle such data. Commented Oct 5, 2017 at 15:15

2 Answers 2

2

Here's one way to produce your adjacency matrix as a 2D Numpy array. It assumes that the input graph data is correct, in particular, that its length is a perfect square.

import numpy as np
graph_data = [
 ['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3],
 ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6],
 ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0],
]
size = np.sqrt(len(graph_data)).astype(np.int)
adjacency_matrix = np.array(graph_data)[:,-1].astype(np.int).reshape(size, size)
print(adjacency_matrix)

output

[[0 5 3]
 [5 0 6]
 [3 6 0]]

The above code also assumes that the graph data is in the proper order, since it ignores the letters. Of course, that's easily handled by sorting the graph data before attempting to convert it to an array. Eg,

graph_data.sort()

Here's a pure Python version that outputs a list of tuples:

graph_data = [
 ['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3],
 ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6],
 ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0],
]
graph_data.sort()
size = int(len(graph_data) ** 0.5)
it = iter(row[-1] for row in graph_data)
print(list(zip(*[it]*size)))

output

[(0, 5, 3), (5, 0, 6), (3, 6, 0)]
answered Oct 5, 2017 at 15:17
Sign up to request clarification or add additional context in comments.

1 Comment

This solution was selected because it was exponentially faster than the other. Specifically, the solution which outputs a list of tuples, which then can be converted into a numpy array. Tested performance with a 1.8 million node example - the other suggestion took 10 seconds, but this solution was an amazing 0.3 seconds
2

You can just slice your array and reshape it:

input= [['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3], ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6], ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0]]
arr = np.array(input)
desiredOutput=arr[:, 2].reshape(3, 3).astype(np.float)
# array([[ 0., 5., 3.],
# [ 5., 0., 6.],
# [ 3., 6., 0.]])
answered Oct 5, 2017 at 15:06

3 Comments

Provided that the entries in input are always in the correct order, i.e. the first two columns do not actually matter... (but if that's not the case, you could just sort the input)
Order is always alphabetical: order by col 1, then col 2.
@tobias_k you're right ... it might not be the case.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.