Re: List representation using element position
- To: mathgroup at smc.vnet.net
- Subject: [mg72526] Re: List representation using element position
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Thu, 4 Jan 2007 07:08:25 -0500 (EST)
- Organization: privat
- References: <enfgem$ss1$1@smc.vnet.net> <enfvj8$6vc$1@smc.vnet.net>
Ray Koopman wrote: > Dr. Wolfgang Hintze wrote: > >>Hello group, >>happy new year to all of you! >> >>This one was put up in a slightly different form by me in March 2006. >>It is now more general and it is lossless with respect to information: >> >>Given a list of integers which may repeat, e.g. >> >>lstIn = {2,3,4,4,2,1,1,5,4} >> >>provide a list of the different values and their respective positions in >>the original list. In the example, >> >>LstOut= { >> {1,{6,7}}, >> {2,{2,5}}, >> {3,{2}}, >> {4,{3,4,9}}, >> {5,{8}} >> } >> >>Who finds the shortest function doing this task in general? >> >>My solution appears 15 lines below >> >>Thanks. >> >>Best regards, >>Wolfgang >>1 >> >> >> >>5 >> >> >> >> >>10 >> >> >> >> >>fPos[lstIn_] := Module[{f = Flatten /@ (Position[lstIn, #1] & ) /@ >>Union[lstIn]}, ({#1, f[[#1]]} & ) /@ Range[Length[f]]] >> >>In[15]:= >>fPos[lstIn] >> >>Out[15]= >>{{1, {6, 7}}, {2, {1, 5}}, {3, {2}}, {4, {3, 4, 9}}, {5, {8}}} >> > > fPoz[lstIn_] := {#[[1,1]],Last/@#}& /@ Split[ > Sort@MapIndexed[Flatten@{##}&,lstIn], #1[[1]]==#2[[1]] ] > > Hello Ray, I'm getting {{1, {6}}, {1, {7}}, {2, {1}}, {2, {5}}, {3, {2}}, {4, {3}}, {4, {4}}, {4, {9}}, {5, {8}}} from your function. Did I miss something here? Regards Wolfgang