Message Boards Message Boards

0
|
5309 Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Cleaning up a list of lists

Posted 9 years ago

Hi folks,

I am trying to modify a list of data. My question is related to an earlier discussion. Let us consider the following list:

testList={{1,2,DateObject[],3},{1,DateObject[],3,DateObject[]},{"a",17}}

I want to delete all sublists, which contain the data type DateObject[]. So the final form would be [edited]:

finalList={{"a",17}}

My approach is the following:

DeleteCases[testList, _DateObject, 2]

This removes all list elements up to level 2, but not the sublist itself!

I would appreciate any help!

Cheers,

Philipp

POSTED BY: Philipp Krönert
6 Replies

Delete

POSTED BY: David Reiss

As of Mathematica version 10.2 you can also do Nothing and get the same result:

In[2]:= testList /. {___, _DateObject, ___} :> Nothing

Out[2]= {{"a", 17}}

Yes, there's Nothing that you can do about it.

POSTED BY: David Reiss

Hi David, thank you for Nothing! I will take your idea, because it has a slightly better performance (I' am working with a list of 40,000 entries). Thank you very much!

POSTED BY: Philipp Krönert

Aw, think Nothing of it. This also works (and you can compare performance):

testList /. {___, _DateObject, ___} :> Sequence[]

and perhaps you will find that nothing is better than Nothing.

POSTED BY: David Reiss
Posted 9 years ago

Philipp,

The Select function takes a test method which it applies to each element in turn, keeping only those that pass the test. So, you could first define a test function:

NoDateObject[a_] := Not[MemberQ[a, _DateObject]]

Then we can simply incorporate this into a Select:

Select[testList, NoDateObject]

This returns {{"a",17}}, which isn't exactly what you wanted above. If you want to keep the sublist structure, then you'd stop here. If you really do want a flat list, then apply Flatten to the result of the Select.

POSTED BY: Eric Rimbey

Thank you very much Eric! It works fine.

POSTED BY: Philipp Krönert
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