Re: case of inconsistent API between Drop and Part?
- To: mathgroup at smc.vnet.net
- Subject: [mg127183] Re: case of inconsistent API between Drop and Part?
- From: Jason Ebaugh <ebaughjason at gmail.com>
- Date: Wed, 4 Jul 2012 03:34:19 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201206300917.FAA01642@smc.vnet.net> <CAEtRDSekECNrpgxeX+MMY9_m8Le6V6F+2BAow=BOqtuSdM+TiQ@mail.gmail.com>
> 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