Message Boards Message Boards

0
|
2252 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Domain patterns in DeleteCases?

Posted 3 years ago

Hi, I'm trying to edit lists using this function, which seems straightforward using the documentation example for deleting integers (shown below). However I'm guessing how to do this for removing even/odd numbers, prime numbers, multiple of a given number, etc., by basically copying the format, but the results below don't make any sense to me, surely because I don't know what I'm doing. Any help would be appreciated!

In[27]:= X = {1.5, 2, 3, 5, 7, 11, 13, 14, 17, 19, 23, 29, 30, 31, 37}

Out[27]= {1.5, 2, 3, 5, 7, 11, 13, 14, 17, 19, 23, 29, 30, 31, 37}

In[28]:= DeleteCases[X, _Integer]

Out[28]= {1.5}

In[29]:= DeleteCases[X, _Even]

Out[29]= {1.5, 2, 3, 5, 7, 11, 13, 14, 17, 19, 23, 29, 30, 31, 37}

In[30]:= DeleteCases[X, _Prime]

Out[30]= {1.5, 2, 3, 5, 7, 11, 13, 14, 17, 19, 23, 29, 30, 31, 37}
POSTED BY: Paul Frisoli
5 Replies
Posted 3 years ago

Another way

DeleteCases[x, _?(Mod[#, 3] == 0 &)]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Not all domains are available for what you are trying to do.

Reading this documentation on numbers might help.

To accomplish the equivalent of deleting the evens you could use this alternative

Select[X,Not[EvenQ[#]]&]

To accomplish the equivalent of deleting the primes you could use this alternative

Select[X,Not[PrimeQ[#]]&]
POSTED BY: Bill Nelson
Posted 3 years ago

Hi Paul,

You can use PatternTest to filter by an arbitrary predicate function. Using built-in ones

DeleteCases[x, _?EvenQ]

DeleteCases[x, _?PrimeQ]

Or an arbitrary one

DeleteCases[x, _?(Sqrt[#] < 4 &)]

DeleteCases[x, _?((IntegerDigits[Round@#] &) /* Total /* EvenQ)]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Many thanks, I will read that section of the documentation.

POSTED BY: Paul Frisoli
Posted 3 years ago

Thanks very much for these additional examples! I can see what I really need to do is wrap my head around how _?, #, & are used, and the classes of numbers.

Using your format, I was able to write this to delete multiples of 3:

DeleteCases[A, _?(#/3 \[Element] Integers &)]
POSTED BY: Updating Name
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract