2
$\begingroup$

https://mathematica.stackexchange.com/questions/256041/selective-removal-from-a-list/256053#256053[1]

In reference to the above question, I apologize but I've posed the question incorrectly. Let me try again.

I have two lists consisting of dates and measurements. I would like to deduplicate the first list by eliminating elements that (partially) match elements in the second.

Here is the first list:

lis1 = {{Today, "a", 2, "b", 2}, {Today, "c", 3, "d", 3}, {Today, "e", 5, "f", 6}, {Today, "g", 7, "h", 8}};

The second list is structured similarly, with the exception that the Date may be any of the 3 days prior to today, say:

lis2 = {{DatePlus[Today,-2],"a","xx","b","xx"},{DatePlus[Today,-1],"e","xx","f","xx"}, {DatePlus[Today,-3],"p","xx","q","xx"}}

I'd like to strike the members of lis1 where the second (a, e) and fourth (b, f) sub-elements of the list members are identical to the second and fourth sub-elements of some member of lis2, resulting in:

res = {{Today, "c", 3, "d", 3},{Today, "g", 7, "h", 8}}

Hope this is clearer!

$\endgroup$

1 Answer 1

3
$\begingroup$
pattern = Alternatives @@ 
   ({Alternatives @@ DateRange[#, DatePlus[#, {3, "Day"}]], #2, _, #4, _} & @@@ lis2);

DeleteCases[lis1, pattern]

enter image description here

MatrixForm /@ {lis1, lis2, DeleteCases[lis1, pattern]}

enter image description here

$\endgroup$
2
  • $\begingroup$ many thanks @kglr $\endgroup$
    – Suite401
    Commented Sep 23, 2021 at 20:41
  • $\begingroup$ @Suite401, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented Sep 23, 2021 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.