# Selecting items in a list based on multiple conditions

This question has been solved. Thanks for all your help!

I have data that looks like this.

{{{1,3}},{{2,3},{5,2},{2,3},{4,1}},{{4,3},{1,1},{3,2},{5,4},{7,6}},{{2,6},{3,3}}}

These represent the position of objects and their trajectories at different times. So, that the first object {{1,3}} only stayed at that one position with coordinates {1,3}. But, the second object {{2,3},{5,2},{2,3},{4,1}} was in 4 positions.

I would like to delete only the objects that moved more than 2 times, and that have the same position at times 1 and 3. So, an example would be:

{{2,3},{5,2},{2,3},{4,1}}

And I would like to keep all the other data.

I would appreciate if some could help with figuring this out.

Thanks!

• @Andres This is actually not multiple conditions: If your objects have the same position at times 1 and 3, it is clear that they moved >2 times :) – yarchik Mar 4 '16 at 9:07
• I noticed that you put "the question has now been solved" at the top of your question. This is not how you are supposed to do it, you are supposed to "accept" an answer by using the check mark next to an answer. This will signal to everybody that you consider the problem solved. – C. E. Mar 11 '16 at 22:56

This should do it:

DeleteCases[list, {x_, _, x_, ___}]

or equivalently

list /. {x_, _, x_, ___} -> Nothing

@Runnykine pointed out corectly that I only address 1 criterion.

DeleteCases[d, _?(Length@# > 2 && #[[1]] == #[[3]] &)]
Pick[d, Or[Length@# < 3, #[[1]] != #[[3]]] & /@ d]
Select[d, Or[Length@# < 3, #[[1]] != #[[3]]] &]

Just some variants:

Pick[d, Length /@ d, _?(# < 3 &)]
Select[d, Length@# < 3 &]
Cases[d, _?(Length@# < 3 &)]

where d is list

• This does not address the second criterion - that the positions at times 1 and 3 be the same for deletion. – RunnyKine Mar 4 '16 at 8:26
• @RunnyKine I missed that. I will address when I am near computer. Thanks – ubpdqn Mar 4 '16 at 8:28
• I think you meant to put that in your answer and not the question. – RunnyKine Mar 4 '16 at 8:47
• @RunnyKine shouldn't do MSE when you are sick...making all sorts of blunders today – ubpdqn Mar 4 '16 at 8:49
• Yeah, I feel you. +1 for your efforts :) – RunnyKine Mar 4 '16 at 8:51

This works as well

Select[
list,
If[Length[#] > 2,
If[#[[1]] == #[[3]],
False,
True
],
True
] &
]

And i think it's easier to understand how it works (well, at least for me).

• If[whatever, False, True] (or the reverse) is usually frowned upon by serious programmers (those that don' t laugh often).They usually suggest things like Select[list, Length[#] < 3 || #[[1]] != #[[3]] &] instead – Dr. belisarius Mar 4 '16 at 2:36
• Won't argue with that. However I wanted to explicitly specify that it is greater than 2. – Andrew S. Mar 4 '16 at 2:42