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

  • Home
  • SEARCH
  • 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 6761315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:21+00:00 2026-05-26T14:10:21+00:00

I have two arrays P and T. P[i] is a number, whose time stamp

  • 0

I have two arrays P and T. P[i] is a number, whose time stamp is T[i]; There might be duplicated time stamps.

I want to produce another two arrays Q and U, where Q[i] has time stamp U[i], and Q[i] is the sum of all elements in P that have time stamp U[i];

For example, for

P = [1, 2, 3, 4, 5]
T = [0, 0, 1, 1, 1]

I will produce

Q = [3, 12]
U = [0, 1];

Is there a fast way of doing this in numpy, that hopefully vectorizes it?

  • 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-26T14:10:22+00:00Added an answer on May 26, 2026 at 2:10 pm

    Using numpy 1.4 or better:

    import numpy as np
    
    P = np.array([1, 2, 3, 4, 5]) 
    T = np.array([0, 0, 1, 1, 1])
    
    U,inverse = np.unique(T,return_inverse=True)
    Q = np.bincount(inverse,weights=P)
    print (Q, U)
    # (array([  3.,  12.]), array([0, 1]))
    

    Please note that this is not the fastest solution. I tested the speed this way:

    import numpy as np
    
    N = 1000
    P = np.repeat(np.array([1, 2, 3, 4, 5]),N)
    T = np.repeat(np.array([0, 0, 1, 1, 1]),N)
    
    def using_bincount():
        U,inverse = np.unique(T,return_inverse=True)
        Q = np.bincount(inverse,weights=P)
        return Q,U
        # (array([  3.,  12.]), array([0, 1]))
    
    def using_lc():
        U = list(set(T))
        Q = [sum([p for (p,t) in zip(P,T) if t == u]) for u in U]
        return Q,U
    
    def using_slice():
        U = np.unique(T)
        Q = np.array([P[T == u].sum() for u in U])
        return Q,U
    

    For small arrays, wim’s solution is faster (N=1):

    % python -mtimeit -s'import test' 'test.using_lc()'
    100000 loops, best of 3: 18.4 usec per loop
    % python -mtimeit -s'import test' 'test.using_slice()'
    10000 loops, best of 3: 66.8 usec per loop
    % python -mtimeit -s'import test' 'test.using_bincount()'
    10000 loops, best of 3: 52.8 usec per loop
    

    For large arrays, joris’s solution is faster (N=1000):

    % python -mtimeit -s'import test' 'test.using_lc()'
    100 loops, best of 3: 9.93 msec per loop
    % python -mtimeit -s'import test' 'test.using_slice()'
    1000 loops, best of 3: 390 usec per loop
    % python -mtimeit -s'import test' 'test.using_bincount()'
    1000 loops, best of 3: 846 usec per loop
    

    I doubt it matters in this case, but benchmarks can change depending on version of numpy, python, OS, or hardware. It would not hurt to repeat these benchmarks on your machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays First array has large number of words, i have 2nd
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays with time values in them in this format. 00:00:00 which
I have two arrays of values like X,Y,Z and 1,2 There is a table
I have two sets of arrays with a variable number of elements, for instance:
I have two JavaScript arrays below that both have the same number of entries,
I have two arrays and I want to check if every element in arr2
I have two PHP arrays. One contains a group name and another contains a
I have two number arrays arrAll and arrGood : arrAll <- c(3:12, 9:3) arrGood
I have two arrays of animals (for example). $array = array( array( 'id' =>

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.