MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List element manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25549] Re: List element manipulation
  • From: adam_smith at my-deja.com
  • Date: Sat, 7 Oct 2000 03:35:53 -0400 (EDT)
  • References: <8r19qf$hvt@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I had tried this post before but it seems to have been lost.

I noted that a couple of people provided the solution:

MapAt[# - 1 &, Range[116, 164, 8], -1]

A similar thing works with ReplacePart and I have found that there can
be a significant speed advantage for ReplacePart as demonstrated below
running Version 4.0.1 on a 333 MHz PII with 192 MByte ram.  For 100,000
long list 0.01 sec. for ReplacePart vs 0.15 sec for MapAt.

Adam Smith

MapAt vs. ReplacePart

For small lists there is no timing difference

In[1]:=
 Timing[
  MapAt[# - 1 &, Range[116, 164, 8], -1] ]

Timing[
  Range[116, 164, 8] // ReplacePart[#, Last[#] - 1 , -1] &]


Out[1]=
{0. Second, {116, 124, 132, 140, 148, 156, 163}}

Out[2]=
{0. Second, {116, 124, 132, 140, 148, 156, 163}}

However, for very large lists ReplacePart is significantly faster and
the speed gain increase the larger the list gets.

Note:  I have suppressed the output and I have only shown the last two
elements of the "replaced" result as a check

In[3]:=
bigmap = Timing[
      MapAt[# - 1 &, Range[1, 100000, 1], -1] ];
bigmap[[1]]
{bigmap[[2, -2]], bigmap[[2, -1]]}
bigreplace = Timing[
      Range[1, 100000, 1] // ReplacePart[#, Last[#] - 1 , -1] & ];
bigreplace[[1]]
{bigreplace[[2, -2]], bigreplace[[2, -1]]}

Out[4]=
0.15 Second

Out[5]=
{99999, 99999}

Out[7]=
0.01 Second

Out[8]=
{99999, 99999}

In article <8r19qf$hvt at smc.vnet.net>,
  "Martin Rommel" <Martin_Rommel at mac.com> wrote:
> I want to decrement the last number in list. This ought to be easy,
but my
> initial attempts did not bear fruits:
>
> In[40]:=
> MapAt[Decrement, Range[116, 166, 8], -1]
>
>     Decrement::"rvalue": "164 is not a variable with a value, so its
value
> cannot be changed."
>
> Out[40]=
> {116, 124, 132, 140, 148, 156, 164--}
>
> Do I need to use ReplacePart? That works but still gives me an error!
>
> In[53]:=
> Range[116, 166, 8] // ReplacePart[#, --Last[#], -1] &
>
>     Set::"write": "Tag Last in Last[{116, 124, 132, 140, 148, 156,
164}] is
> Protected."
>
> Out[53]=
> {116, 124, 132, 140, 148, 156, 163}
>
> Different error here:
>
> In[51]:=
> Range[116, 166, 8] // ReplacePart[#, --#[[-1]], -1] &
>
>     Set::"setps": "{116, 124, 132, 140, 148, 156, 164} in assignment
of part
> is not a symbol."
>
> Out[51]=
> {116, 124, 132, 140, 148, 156, 163}
>
> Anybody out there able to enlighten me?
>
> Thanks, Martin
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.


  • Prev by Date: Re: ? D[f,{x,n}]
  • Next by Date: Re: ? D[f,{x,n}]
  • Previous by thread: Re: List element manipulation
  • Next by thread: Re: extracting common items from a list