MathGroup Archive 2004

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

Search the Archive

Re: A list element replacement.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50924] Re: [mg50887] A list element replacement.
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 28 Sep 2004 00:58:34 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Robert G. Wilson v [mailto:rgwv at rgwv.com]
To: mathgroup at smc.vnet.net
>Sent: Sunday, September 26, 2004 11:32 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg50924] [mg50887] A list element replacement.
>
>
>HELP please,
>
>I have a list of many elements, some of which are identical, and wish
>to 'replace' elements in the first list with elements of a second list
>of unequal length.
>
>As an example, list 1 might equal {3, 4, 5, 6, 6, 6, 9, 2, 1} 
>and if the
>elements {9, 3, 1} are present in list 1, when replace them 
>with elements
>{2, 4}. Thus list 1 would become {4, 5, 6, 6, 6, 2, 2, 4}. I am not
>interested in order for I well use Sort at the end for presentation.
>
>Thank you in advance. Bob.
>
>

Certainly, I do not quite understand, what you want, e.g. what is list 2?

I assume that you have a list of "replacements" el to rep, where in your example 

In[25]:= el = Sort[{9, 3, 1}];
In[26]:= rep = {2, 4};

Now you may build up a function based on this elementary substitution

In[39]:=
With[{ss = Table[Unique[s], {Length[el]}]}, 
  Sort[list1] /. 
        Rule @@ {Flatten[
              Prepend[Transpose[{el, Function[s, s___] /@ ss}], s$0___]], 
            Join[{s$0}, rep, ss]}] // Sort
Out[39]=
{2, 2, 4, 4, 5, 6, 6, 6}

You have to Fold that over your list of replacements (if I understand you right).
The application of rule is done to avoid interference with scoping for Rule when evaluating its components.


--
Hartmut Wolf


  • Prev by Date: Re: problem with very slow matrix function
  • Next by Date: Re: Export to file
  • Previous by thread: Re: A list element replacement.
  • Next by thread: Array construction