Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:38:16+00:00 2026-05-16T01:38:16+00:00

I have a very large numpy array (containing up to a million elements) like

  • 0

I have a very large numpy array (containing up to a million elements) like the one below:

[0,1,6,5,1,2,7,6,2,3,8,7,3,4,9,8,5,6,11,10,6,7,12,11,7,
8,13,12,8,9,14,13,10,11,16,15,11,12,17,16,12,13,18,17,13,
14,19,18,15,16,21,20,16,17,22,21,17,18,23,22,18,19,24,23]

and a small dictionary map for replacing some of the elements in the above array

{4: 0, 9: 5, 14: 10, 19: 15, 20: 0, 21: 1, 22: 2, 23: 3, 24: 0}

I would like to replace some of the elements according to the map above. The numpy array is really large, and only a small subset of the elements (occurring as keys in the dictionary) will be replaced with the corresponding values. What is the fastest way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:38:17+00:00Added an answer on May 16, 2026 at 1:38 am

    I believe there’s even more efficient method, but for now, try

    from numpy import copy
    
    newArray = copy(theArray)
    for k, v in d.iteritems(): newArray[theArray==k] = v
    

    Microbenchmark and test for correctness:

    #!/usr/bin/env python2.7
    
    from numpy import copy, random, arange
    
    random.seed(0)
    data = random.randint(30, size=10**5)
    
    d = {4: 0, 9: 5, 14: 10, 19: 15, 20: 0, 21: 1, 22: 2, 23: 3, 24: 0}
    dk = d.keys()
    dv = d.values()
    
    def f1(a, d):
        b = copy(a)
        for k, v in d.iteritems():
            b[a==k] = v
        return b
    
    def f2(a, d):
        for i in xrange(len(a)):
            a[i] = d.get(a[i], a[i])
        return a
    
    def f3(a, dk, dv):
        mp = arange(0, max(a)+1)
        mp[dk] = dv
        return mp[a]
    
    
    a = copy(data)
    res = f2(a, d)
    
    assert (f1(data, d) == res).all()
    assert (f3(data, dk, dv) == res).all()
    

    Result:

    $ python2.7 -m timeit -s 'from w import f1,f3,data,d,dk,dv' 'f1(data,d)'
    100 loops, best of 3: 6.15 msec per loop
    
    $ python2.7 -m timeit -s 'from w import f1,f3,data,d,dk,dv' 'f3(data,dk,dv)'
    100 loops, best of 3: 19.6 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're not allowed to wrap a block level element in… May 16, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer If SVN is able to merge changes, it won't cause… May 16, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer Ok - so notice that the parameter to your method… May 16, 2026 at 6:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a program which needs to turn many large one-dimensional numpy arrays of
I have a very large array of floating point image data: In[25]:= Dimensions[daylightImgd] Out[25]=
I have a very large array I need to check for debugging purposes, problem
I have very large images (jpg) and i want to write a csharp program
I have very large website which uses a lot of cookies. There are approx.
I need help with regular expression. I have very large collection of text files
I have a very large navigation bar on my website and I was given
I have a very large table (5mm records). I'm trying to obfuscate the table's
I have a very large database I need to diagram. The database is SQL
I have a very large set of binary files where several thousand raw frames

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.