Skip to content
Advertisement

Create NumPy array from list of tuples

I have data in the following format:

JavaScript

And I want use this information to create a NumPy array that has the value 1.0 in position 2, value 2.5 in position 6, etc. All positions not listed in the above should be zeroes. Like this:

JavaScript

Advertisement

Answer

First reformat the data:

JavaScript

And then create the array:

JavaScript

Note that you need to convert indices to a list, otherwise when using it for indexing numpy will think it is trying to index multiple dimensions.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement