1
$\begingroup$

I have data in the following form

    data = {{A,Null,C,D},{A,B,Null,D},{A,Null,C,Null},{A,B,C,D}}

I want to identify entries 1-3 in that list and delete them, since they are already included in the 4th entry, which is the most complete one.

My first idea was to delete the Null entries and search if that shortened entry is included in another entry, but I can't get it to work.

$\endgroup$
3
  • $\begingroup$ If {A,B,C,D} is not present in the list what do you expect as the output ? $\endgroup$
    – Lotus
    Feb 6 '20 at 10:31
  • $\begingroup$ As per your suggestion: If[SubsetQ[Last@data,#],Sequence[],#]& /@ data /. Null -> Sequence[] works in this case, but as @Lotus pointed out, it is not clear what output do you expect when {A,B,C,D} is not there $\endgroup$
    – amator2357
    Feb 6 '20 at 10:33
  • $\begingroup$ For {{A, Null,C, Null},{A, Null, C, D}} I'd like to retain {A, Null, C, D} $\endgroup$
    – testrado
    Feb 6 '20 at 10:49
1
$\begingroup$
data = {{a, Null, c, d}, {a, b, Null, d}, {a, Null, c, Null}, {a, b, c, d}};

DeleteDuplicates[Reverse@SortBy[DeleteCases[#, Null] &] @ data, 
   SubsetQ @@ (DeleteCases[#, Null] & /@ {##}) &]

{{a, b, c, d}}

data2 = {{a, b, c, Null}, {a, Null, c, Null}, {a, b, c, Null}, {a, Null, c, d}};

DeleteDuplicates[Reverse@SortBy[DeleteCases[#, Null] &] @ data2, 
 SubsetQ @@ (DeleteCases[#, Null] & /@ {##}) &]

{{a, Null, c, d}, {a, b, c, Null}}

$\endgroup$
3
  • $\begingroup$ This look fantastic already, thanks. Would it be possible wih this method to retain the structure of the list in cases like: {{a,b,c,Null},{a,Null,c,Null},{a,b,c,Null},{a,Null,c,d}}? This returns {{a,c,d},{a,b,c}}, I need {{a,c,Null,d},{a,b,c,Null}}. I guess I could look up every element in the original list and put it in the right column but maybe there is an easier way. $\endgroup$
    – testrado
    Feb 6 '20 at 10:57
  • $\begingroup$ @testrado, please see the updated version. $\endgroup$
    – kglr
    Feb 6 '20 at 11:06
  • $\begingroup$ It took me a while to completely understand that line of code, but I just ran it on the data, it seems that it worked perfectly. Many thanks! $\endgroup$
    – testrado
    Feb 6 '20 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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