0

I have a dictionary of the following format:

{1: 7, 2: 1, 4: 2, 5: 1, ...}

The dictionary can be potentially very large, so I decided to use numpy library for more efficient work with vectors.

Now I need to insert values from the above dictionary in a numpy vector (1-dimensional array) at the key index. So tried naively the following:

import numpy as np

d = {1: 7, 2: 1, 4: 2, 5: 1}
T = np.zeros(shape=(n), dtype=np.int16)

for k, v in d.items():
    N = np.insert(T, k, v)

print(N)

However insert method always returns new array, and overwrites N in my example. So what is the right way to change array elements, just as I'd do in C, e.g. T[0] = 1.

Looks like numpy does not allow this?

Thanks.

UPDATE As suggested by Divakar, the solution is to assign multiple values at once:

T[list(d.keys())] = list(d.values())

And this does not require a loop.

7
  • What is the n parameter? Commented Nov 29, 2019 at 16:17
  • 2
    T[list(d.keys())] = list(d.values())? Commented Nov 29, 2019 at 16:18
  • @Divakar, thanks a lot. This is indeed very elegant solution, Where does it say that numpy array object can take a list as parameter? Commented Nov 29, 2019 at 16:30
  • Not sure how you were lead to numpy array object taking a list as parameter. It's a simple integer based indexding - docs.scipy.org/doc/numpy/reference/… Commented Nov 29, 2019 at 16:32
  • @Divakar, I'm newbie in Python. In your comment you did list(d.keys()) which returns a list of keys from dictionary, and then submit it as index to T object. Commented Nov 29, 2019 at 16:44

1 Answer 1

3

In numpy np.insert makes a new array by splitting the original array and inserting one or more values someplace in the middle.

In [193]: arr = np.arange(6)                                                    
In [194]: arr                                                                   
Out[194]: array([0, 1, 2, 3, 4, 5])
In [195]: np.insert(arr, 2, 100)                                                
Out[195]: array([  0,   1, 100,   2,   3,   4,   5])

Sometimes people talk about adding a value to an array, but `+' is elementwise numeric addition

In [196]: arr + 10                                                              
Out[196]: array([10, 11, 12, 13, 14, 15])
In [197]: 'one '+'two'        # string 'addition'                                                          
Out[197]: 'one two'

'assign a value(s)' is probably the clearest description of what you want to do. You want to modify an element of the original array, without changing its size.

In [198]: arr[3] = 100                                                          
In [199]: arr                                                                   
Out[199]: array([  0,   1,   2, 100,   4,   5])

@Divakar's idea assigns multiple values at once:

In [200]: arr[[1,2]] = [100,200]                                                
In [201]: arr                                                                   
Out[201]: array([  0, 100, 200, 100,   4,   5])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.