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 6653455
In Process

The Archive Base Latest Questions

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

A question from a complete Python novice. I have a column array where I

  • 0

A question from a complete Python novice.

I have a column array where I need to force certain values to zero depending on a conditional statement applied to another array. I have found two solutions, which both provide the correct answer. But they are both quite time consuming for the larger arrays I typically need (>1E6 elements) – also I suspect that it is poor programming technique. The two versions are:

from numpy import zeros,abs,multiply,array,reshape

def testA(y, f, FC1, FC2):
    c = zeros((len(f),1))
    for n in xrange(len(f)):
        if abs(f[n,0]) >= FC1 and abs(f[n,0]) <= FC2:
            c[n,0] = 1.
    w = multiply(c,y)
    return w

def testB(y, f, FC1, FC2):
    z = [(abs(f[n,0])>=FC1 and abs(f[n,0])<=FC2) for n in xrange(len(f))]
    z = multiply(array(z,dtype=float).reshape(len(f),1), y)
    return z

The input arrays are column arrays as this matches the post processing to be done. The test can be done like:

>>> from numpy.random import normal as randn
>>> fs, N = 1.E3, 2**22
>>> f = fs/N*arange(N).reshape((N,1))
>>> x = randn(size=(N,1))
>>> w1 = testA(x,f,200.,550.)
>>> z1 = testB(x,f,200.,550.)

On my laptop testA takes 18.7 seconds and testB takes 19.3 – both for N=2**22. In testB I also tried to include “z = [None]*len(f)” to preallocate as suggested in another thread but this doesn’t really make any difference.

I have two questions, which I hope to have the same answer:

  1. What is the “correct” Python solution to this problem?
  2. Is there anything I can do to get the answer faster?

I have deliberately not used any time at all using compiled Python for example – I wanted to have some working code first. Hopefully also something, which is good Python style. I hope to be able to get the execution time for N=2**22 below two seconds or so. This particular operation will be used many times so the execution time does matter.

I apologize in advance if the question is stupid – I haven’t been able to find an answer in the overwhelming amount of not always easily accessible Python documentation or in another thread.

  • 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-26T01:16:18+00:00Added an answer on May 26, 2026 at 1:16 am

    use bool array to access elements in array y:

    def testC(y, f, FC1, FC2):
        f2 = abs(f)
        idx = (f2>=FC1) & (f2<=FC2)
        y[~idx] = 0
        return y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to complete a practice question from a book on generics but the
The question from title doesn't need more details.
This is a continuation question from a previous question I have asked I now
I have a python program that does something like this: Read a row from
Question from beginner. Why code <%= System.DateTime.Now.ToLongDateString() %> is not executed in ASP.NET. Could
First question from me; I'm currently fixing a graphing service that uses XSLFO to
So I got this question from one of the developers in my team: What
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I am asking this question from an educational/hacking point of view, (I wouldn't really
I got this question from this discussion . A method call like object.m does

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.