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


Groups > comp.soft-sys.math.mathematica > #10778

Re: case of inconsistent API between Drop and Part?

From Jason Ebaugh <ebaughjason@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: case of inconsistent API between Drop and Part?
Date 2012-07-04 07:33 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jt0rke$1e5$1@smc.vnet.net> (permalink)
References <201206300917.FAA01642@smc.vnet.net> <CAEtRDSekECNrpgxeX+MMY9_m8Le6V6F+2BAow=BOqtuSdM+TiQ@mail.gmail.com>

Show all headers | View raw


> Use one call to Drop to remove only elements
> 
>        2,3,7,11, and 12
> 
> from
> 
> lis={q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m}


This will do the trick:

list = {q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, h, j, k, l, z, x, c, v, b, n, m};

ElementsToRemove = {2, 3, 7, 11, 12};
ElementsToRemove = Table[Rule[element, Null], {element, ElementsToRemove}]

Out[]= {2 -> Null, 3 -> Null, 7 -> Null, 11 -> Null, 12 -> Null}


DeleteCases[ReplacePart[list, ElementsToRemove], Null]

Out[]= {q, r, t, y, i, o, p, d, f, g, h, j, k, l, z, x, c, v, b, n, m}


The above can be put together into one function:

DropElements[list_, elementstoremove_] := 
 DeleteCases[
  ReplacePart[list, 
   Table[Rule[element, Null], {element, elementstoremove}]], Null]


newlist = CharacterRange["a", "z"]

Out[]= {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", \
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}


NewElementsToRemove = {1, 4, 5, 9, 10, 11, 13, 20, 25};


DropElements[newlist, NewElementsToRemove]

Out[]= {"b", "c", "f", "g", "h", "l", "n", "o", "p", "q", "r", "s", "u", "v", "w", "x", "z"}



Using the Null intermediate is a bit sloppy. But this will at least get you going in the right direction to make a more solid version. 



Jason Ebaugh

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: case of inconsistent API between Drop and Part? Jason Ebaugh <ebaughjason@gmail.com> - 2012-07-04 07:33 +0000

csiph-web