Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: List representation using element position

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72513] Re: [mg72506] List representation using element position
  • From: Adriano Pascoletti <pascolet at dimi.uniud.it>
  • Date: Wed, 3 Jan 2007 05:28:39 -0500 (EST)
  • References: <200701030639.BAA01288@smc.vnet.net>

Wolfgang,
here is a shorter solution based on Sow and Reap

In[1]:=
lstIn = {2, 3, 4, 4, 2, 1, 1, 5, 4};

In[2]:=
Sort@Last@Reap[MapIndexed[Sow[#2[[1]], #1] & , lstIn], _,List]

Out[2]=
{{1, {6, 7}}, {2, {1, 5}}, {3, {2}}, {4, {3, 4, 9}},
   {5, {8}}}

Adriano Pascoletti

On 3 gen 2007, at 07:39, 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}}}
>


  • Prev by Date: Re: Re: programming problem about elements taken
  • Next by Date: Re: glibc version problem
  • Previous by thread: List representation using element position
  • Next by thread: Re: List representation using element position