Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #14847 > unrolled thread

Re: Removing list elements in x by list y

Started byAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
First post2013-06-14 08:48 +0000
Last post2013-06-14 08:48 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Removing list elements in x by list y Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-06-14 08:48 +0000

#14847 — Re: Removing list elements in x by list y

FromAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
Date2013-06-14 08:48 +0000
SubjectRe: Removing list elements in x by list y
Message-ID<kpeldo$hrf$1@smc.vnet.net>
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

Hi, Wolfgang,

One way to do this is as follows:

x = {u, v, w, w, d};
y = {u, d};

Delete[x, Flatten[Map[Position[x, #] &, y], 1]]

{v, w, w}

This is another, rule-based way:

x /. a_ /; MemberQ[y, a] -> Sequence[]

{v, w, w}

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu



[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web