Skip to content
Advertisement

Create NumPy array from list of tuples

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])

Advertisement

Answer

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.

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