Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'boolean': 0.16; 'hdf': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statement.': 0.16; 'vals': 0.16; 'wrote:': 0.16; 'input': 0.18; 'first,': 0.20; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'exclude': 0.29; 'array': 0.29; 'convert': 0.29; '[1]': 0.32; 'maybe': 0.33; 'optimize': 0.33; 'list': 0.34; 'ones': 0.35; 'there': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; '12,': 0.37; 'data': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'close': 0.61; "you'll": 0.61; 'subject: ': 0.61; 'skip:n 10': 0.62; 'note:': 0.66; 'received:130': 0.73; 'from:addr:jeremy': 0.84; 'subject:check': 0.84; 'subject:over': 0.84; 'subject:value': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Jeremy Sanders Subject: Re: Optimizing if statement check over a numpy value Date: Thu, 23 Jul 2015 13:42:31 +0200 References: <65c45685-dee1-41f8-a16a-7a062f4e7b02@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: lap75107.mpe.mpg.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437651747 news.xs4all.nl 2856 [2001:888:2000:d::a6]:49215 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94444 Heli Nix wrote: > Is there any way that I can optimize this if statement. Array processing is much faster in numpy. Maybe this is close to what you want import numpy as N # input data vals = N.array([42, 1, 5, 3.14, 53, 1, 12, 11, 1]) # list of items to exclude exclude = [1] # convert to a boolean array exclbool = N.zeros(vals.shape, dtype=bool) exclbool[exclude] = True # do replacement ones = vals==1.0 # Note: ~ is numpy.logical_not vals[ones & (~exclbool)] = 1e-20 I think you'll have to convert your HDF array into a numpy array first, using numpy.array(). Jeremy