MathGroup Archive 2010

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

Search the Archive

Re: List of multiple elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112074] Re: List of multiple elements
  • From: "Dr. Wolfgang Hintze" <weh at snafu.de>
  • Date: Sun, 29 Aug 2010 02:49:36 -0400 (EDT)
  • References: <i57rm6$1fa$1@smc.vnet.net>

"Bill Rowe" <readnews at sbcglobal.net> schrieb im Newsbeitrag 
news:i57rm6$1fa$1 at smc.vnet.net...
> On 8/26/10 at 6:48 AM, weh at snafu.de (Dr. Wolfgang Hintze) wrote:
>
>>Given the list
>
>>a={1,1,2,1,2};
>
>>we can reduce multiple instances of elements using
>
>>b=Union[a] {1,2}
>
>>The question is now how to compute the list of multiple elements. In
>>our example this would be m={1,1,2}.
>
>>A possible solution is
>
>>m[x_]:= Flatten[Take[#, {1, Length[#] - 1}] & /@ Select[Split[x],
>>Length[#] > 1 &]]
>
>>m[a] {1,1,2}
>
>>I'm sure there is a much more elegant solution. Can you suggest one?
>
> The function you have included cannot possibly give the result
> you indicate. With the particular example you start with Split
> will only create one sub-list of length greater than 1 which is
> {1,1}. Since you only select sub-lists with length greater than
> 1, you effectively drop all elements not equal to 1 from the
> original list. Consequently, you cannot end up with what you
> have indicated.
>
> The following will obtain the result you have indicated you want.
>
> In[13]:= b = Union[a];
> =46latten@DeleteDuplicates[a //. {x__, Sequence @@ b, y___} :> {x,
> b, y}]
>
> Out[14]= {1,1,2}
>
> But, it is far from clear to me this will scale in an
> intelligent way to deal with a more complex case.
>
> The simplest way I can think of to get the desired end result
> would be something like
>
> a[[;;3]]
>
> or
>
> Drop[a,-2]
>
> but these clearly aren't general solutions
>
Bill, you are right, I made a mistake forgetting the Sort

m1[x_]:= Flatten[Take[#, {1, Length[#] - 1}] & /@ 
Select[Split[Sort[x]], Length[#] > 1 &]]

Derivation by example


x={1, 1, 2, 3, 1, 3}
Sort[x]
{1, 1, 1, 2, 3, 3}
Split[%]
{{1, 1, 1}, {2}, {3, 3}}
Select[%, Length[#] > 1 &]
{{1, 1, 1}, {3, 3}}
Take[#, {1, Length[#] - 1}] & /@ %
Out[77]=
{{1, 1}, {3}}
In[78]:=
Flatten[%]
Out[78]=
{1, 1, 3}

Regards,
Wolfgang




  • Prev by Date: Re: Plot of (2 x^2 - x^3)^(1/3)
  • Next by Date: More about Problems with Module
  • Previous by thread: Re: List of multiple elements
  • Next by thread: Re: List of multiple elements