Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

Re: Removing list elements in x by list y

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131177] Re: Removing list elements in x by list y
  • From: Dana DeLouis <dana01 at icloud.com>
  • Date: Sat, 15 Jun 2013 04:25:48 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net

> I wish to "subtract" y from x, i.e. remove exactly the elements in x

Hello.  I may be wrong, but if you have 10 each of everything, and wanted to subtract 8 of each item, you should be left with 2 items of each.

x=Table[{a,b,c,d},{10}]//Flatten//Sort;
y=Table[{a,b,c,d},{8}]//Flatten//Sort;

Select[x,FreeQ[y,#]&]
{}
Cases[x,_?(FreeQ[y,#]&)]
{}
Pick[x,FreeQ[y,#]&/@x]
{}
Delete[x,Flatten[Map[Position[x,#]&,y],1]]
{}
x/.a_/;MemberQ[y,a]->Sequence[]
{}
Delete[x,Flatten[Position[x,#]&/@y,1]]
{}
DeleteCases[x,Alternatives@@y]
{}
Select[x,!MemberQ[y,#]&]
{}

I believe the answer should be:

Fold[DeleteCases[#1,#2,1,1]&,x,y]
{a,a,b,b,c,c,d,d}


















On Thursday, June 13, 2013 2:31:35 AM UTC-4, Dr. Wolfgang Hintze wrote:
> I have simple question.
>
>
>
> Given two lists
>
>
>
> x = {u, v, w, w, d};
>
> y = {u, d};
>
>
>
> I wish to "subtract" y from x, i.e. remove exactly the elements in x
>
> which belong to y giving
>
>
>
> x "-" y = {v,w,w}
>
>
>
> Complement[] gives instead {v,w}.
>
> Note that we are talking about lists rather than sets.
>
>
>
> I'm sure there is a simple standard command, but I can't find it.
>
> Can you?
>
>
>
> Regards,
>
> Wolfgang





  • Prev by Date: How does one get data out of a TemporalData object?
  • Next by Date: computation of two-point objects
  • Previous by thread: Re: Removing list elements in x by list y
  • Next by thread: Position