MathGroup Archive 2010

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

Search the Archive

Re: List of multiple elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112047] Re: List of multiple elements
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 27 Aug 2010 04:06:35 -0400 (EDT)

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



  • Prev by Date: Problems with Module
  • Next by Date: Re: NotebookAutoSave?
  • Previous by thread: Re: List of multiple elements
  • Next by thread: Re: List of multiple elements