Message Boards Message Boards

GROUPS:

Domain patterns in DeleteCases?

Posted 1 month ago
403 Views
|
5 Replies
|
4 Total Likes
|

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}
5 Replies
Posted 1 month 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 1 month ago

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

Posted 1 month 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 1 month 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 1 month ago

Another way

DeleteCases[x, _?(Mod[#, 3] == 0 &)]
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