Re: Re: Replace list element based on a
- To: mathgroup at smc.vnet.net
- Subject: [mg106706] Re: [mg106652] Re: [mg106630] Replace list element based on a
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 21 Jan 2010 04:55:48 -0500 (EST)
- References: <201001191013.FAA29062@smc.vnet.net> <4B55E3E4.1020804@wolfram.com>
Hi Kurt, just in case you have really large datasets to work on, the rule-based solution is not the most efficient. Here is another one, which worked on my tests about 40 times faster: Clear[fncHourFixAlt]; fncHourFixAlt[ hours_List] := ((hours - 6)*# + (hours - 6 + 24)*(1 - #)) &@ UnitStep[hours - 6]; In[17]:= lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3} Out[17]= {18, 19, 20, 21, 22, 23, 0, 1, 2, 3} In[18]:= lst1 /. n_Integer :> fncHourFix[n] Out[18]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21} In[19]:= fncHourFixAlt@lst1 Out[19]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21} In[20]:= largelst = RandomInteger[{0, 23}, 500000]; In[21]:= (res1 = largelst /. n_Integer :> fncHourFix[n]); // Timing Out[21]= {1.25, Null} In[25]:= (res2 = fncHourFixAlt[largelst]); // Timing Out[25]= {0.031, Null} In[23]:= res1 === res2 Out[23]= True Regards, Leonid On Wed, Jan 20, 2010 at 2:48 PM, Canopus56 <canopus56 at yahoo.com> wrote: > Thanks to all for the many replies in this thread. All the replies has me > on the right track. - Kurt > >
- References:
- Replace list element based on a condition how to
- From: Canopus56 <canopus56@yahoo.com>
- Replace list element based on a condition how to