Message Boards Message Boards

0
|
4053 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Deleting elements of a list

Posted 10 years ago

All the elements in my list have decimals however some have digits to the right of the decimal. I would like to create a set with the elements without digits to the right of the decimal. How?

POSTED BY: dgdg grgrg
2 Replies

many ways to do this. Another possibility, using David's input for test

testList = {1.2, 2.0, 2.0001, 4537.0};
Pick[testList, FractionalPart[#] > 0 & /@ testList]
(*{1.2, 2.0001}*)

But this method, and also David's will not catch cases like 4537.000000000000000001 since FractionalPart[4537.000000000000000001] is effectively zero. So if you want to also check for such cases, a more robust test is needed. One way is to increase the Precision (there might be better ways)

testList = {1.2, 2.0, 2.0001, 4537.000000000000000001};
Pick[testList, FractionalPart[SetPrecision[#, 100]] > 0 & /@ testList]
(*{1.2, 2.0001, 4537.000000000000000001}*)
POSTED BY: Nasser M. Abbasi

Convert numbers with no FractionalPart to an empty Sequences, which effectively eliminates them.

testList = {1.2, 2.0, 2.0001, 4537.0};

testList /. x_ /; NumberQ[x] && FractionalPart[x] == 0 -> Sequence[]
{1.2, 2.0001}

Look up Sequence in Help.

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