Re: position of letters in a string
- To: mathgroup at smc.vnet.net
- Subject: [mg53838] Re: [mg53803] position of letters in a string
- From: János <janos.lobb at yale.edu>
- Date: Sat, 29 Jan 2005 06:02:46 -0500 (EST)
- References: <200501280743.CAA00311@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Jan 28, 2005, at 2:43 AM, zak wrote: > in the list of strings: > lst={"catcatz","zrzttys"} > i want some procedure to give the letters in every string a number > depending on its position and discarding the recurrent letters such > that: > first string: c=1; a=2; t=3; z=4 > second string: z=1; r=2; t=3; y=4; s=5 > zak > In the Help of Union there is an UnsortedUnion function. I use that one here. In[22]:= lst = {"catcatz", "zrzttys"} Out[22]= {"catcatz", "zrzttys"} In[26]:= UnsortedUnion[x_] := Module[{f}, f[y_] := (f[y] = Sequence[ ]; y); f /@ x]; In[47]:= lst1 = (UnsortedUnion[ Characters[#1]] & ) /@ lst Out[47]= {{"c", "a", "t", "z"}, {"z", "r", "t", "y", "s"}} In[66]:= pairedLst = First[ Last[Reap[While[lst1 =!= {}, current = First[lst1]; lst1 = Rest[lst1]; Sow[({#1, Flatten[ Position[current, #1]]} & ) /@ current]; ]]]] Out[66]= {{{"c", {1}}, {"a", {2}}, {"t", {3}}, {"z", {4}}}, {{"z", {1}}, {"r", {2}}, {"t", {3}}, {"y", {4}}, {"s", {5}}}} I am sure there are shorter versions. Flatten is not needed if you need their Positions right away. János ---------------------------------------------- Trying to argue with a politician is like lifting up the head of a corpse. (S. Lem: His Master Voice)
- References:
- position of letters in a string
- From: zak <chocolatez@gmail.com>
- position of letters in a string