Re: position of letters in a string
- To: mathgroup at smc.vnet.net
- Subject: [mg53844] Re: position of letters in a string
- From: bghiggins at ucdavis.edu
- Date: Sat, 29 Jan 2005 06:02:54 -0500 (EST)
- References: <ctcr1b$24v$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Zak, Here is one way to assign a number to a letter in string based on
your criterion:
UnsortedUnion[x_]:=Module[{f},f[y_]:=(f[y]=Sequence[];y);f/@x]
Map[MapIndexed[Rule[#1,First[#2]]&,UnsortedUnion[Characters[#]]]&,lst]
{{"c" -> 1, "a" -> 2, "t" -> 3, "z" -> 4},
{"z" -> 1, "r" -> 2, "t" -> 3, "y" -> 4, "s" -> 5}}
Note you cannot assign a string varable a value but if you want the
string "catz" be represented by "1234" and zrtys by "12345" you can
use
(StringJoin[StringReplace[UnsortedUnion[Characters[#1]],
MapIndexed[#1 -> ToString[First[#2]] & ,
UnsortedUnion[Characters[#1]]]]] & ) /@ lst
{"1234", "12345"}
Cheers,
Brian
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