Home > database >  Create NumPy array from list of tuples
Create NumPy array from list of tuples

Time:07-05

I have data in the following format:

[('user_1', 2, 1.0),
 ('user_2', 6, 2.5),
 ('user_3', 9, 3.0),
 ('user_4', 1, 3.0)]

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:

array([0, 3.0, 0, 0, 0, 0, 2.5, 0, 0, 3.0])

CodePudding user response:

First reformat the data:

data = [
    ("user_1", 2, 1.0),
    ("user_2", 6, 2.5),
    ("user_3", 9, 3.0),
    ("user_4", 1, 3.0),
]

usernames, indices, values = zip(*data)

And then create the array:

length = max(indices)   1

arr = np.zeros(shape=(length,))
arr[list(indices)] = values

print(arr)  #  array([0. , 3. , 1. , 0. , 0. , 0. , 2.5, 0. , 0. , 3. ])

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.

CodePudding user response:

I've come up with this solution:

import numpy as np 

a = [('user_1', 2, 1.0),
('user_2', 6, 2.5),
('user_3', 9, 3.0),
('user_4', 1, 3.0)]


res = np.zeros(max(x[1] for x in a) 1)
for i in range(len(a)):
    res[a[i][1]] = a[i][2]

res
# array([0. , 3. , 1. , 0. , 0. , 0. , 2.5, 0. , 0. , 3. ])

First I create a 0 filled array with maximum value of the number in index 1 of each tuple in list a 1 to account that your positions are 1 higher than the indexes inside the array are.

Then I do a simple loop and assign the values according to the arguments in the tuple.

  • Related