0

I'm using python 3.6 .I have a numpy array let's say a.It's in this format

array(['0', '1', '2', ..., '3304686', '3304687', '3304688'],
      dtype='<U7')

I have another dictionary b={1: '012', 2: '023', 3: '045',.....3304688:'01288'}

I want to retrieve each value of b and store it in another numpy array by providing the value of a as a key to b. I was planning to try in this way

    z_array = np.array([])  

    for i in range(a.shape[0]):
        z=b[i]        
        z_array=np.append(z_array,z)

But looking the shape of a,I'm feeling that it will be very much time consuming. Can you please suggest me some alternate approach which will be time efficient?

5
  • 2
    do not use z_array=np.append( in a loop. That gives you quadratic time. Instead, initailize z_array = np.zeros_like(a) Then loop and assign to z_array[i] = b[i] Commented Dec 12, 2017 at 19:52
  • 1
    Why are you working with array of strings anway? You are essentially giving up the speed/space-benefits of numpy arrays. Your strings look like they represent numbers, why not numbers? Commented Dec 12, 2017 at 19:54
  • 4
    Are the array values strings? If so, why aren't the keys of dict strings too? Also, shouldn't that be for i in a:? And what if the keys are not available? Commented Dec 12, 2017 at 19:56
  • Thanks @juanpa.arrivillaga, I will try that. Commented Dec 13, 2017 at 6:55
  • @juanpa.arrivillaga.Instead of np.append,I used your approach & it works. Thanks a lot. Commented Dec 13, 2017 at 11:03

1 Answer 1

1

You could use np.frompyfunc, note that this will create an object array.

b = {str(i): i**3 for i in range(10**7)}
a = [str(i) for i in range(10**7)]
c = np.frompyfunc(b.__getitem__, 1, 1)(a)

or

c = np.frompyfunc(b.get, 1, 1)(a)

to indicate missing keys by None.

In the example with 10,000,000 items and as many lookups takes just a second or two. (Creating a and b takes longer)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Paul Panzer, I will try this

Your Answer

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.