# list management, deletion of duplicates

Rephrasing poorly asked question

This analogy might convey what I'm aiming for:

Let's say this weekend there are sporting events played on Saturday and Sunday.

Let's also say that there are two different wire services or newspapers or whatever that provide game scores, source A that provides the date and the score for each game, and source B that only provides the game scores, but not the exact day (Saturday or Sunday) the game was played.

A source A record could look like:

lisA = {Saturday, team1, score 1, team2, score2}


The source B record for the same game would be:

lisB = {team1, score1, team2, score2}


We combine the two lists, and find pairs of games with identical participants and scores, and simply delete the source B record from the combined list for such games.

If a pair of records exist where the scores are different (but the participants are the same), both records will stay in the combined list, and the Source A date (Saturday or Sunday) gets prepended to the Source B record.

Also it is possible source B that has a contest not listed by source A; that record stays in the combined list.

I hope this time it's unambiguous, and I appreciate your patience and ideas.

• This DeleteCases[lisB, Alternatives @@ Map[Rest, lisA]] deletes games from lisB that are identical with games in lisA, without the day information. IF you could generate example lisA and lisB that would include all your combinations and then show what the result should be that might make it easier to verify an answer matches your conditions and to possibly compare answers.
– Bill
Sep 3 '18 at 21:24
• Thanks, Bill. Here are sample lisA, lisB and res: lisA = {{DateObject[{2000,1,1}],"a",1,"b",2},{DateObject[{2000,1,1}],"c",3,"d",4},{DateObject[{1999,12,31}],"e",5,"f",6}} and lisB = {{"a",1,"b",2},{"c",3,"d",5},{"g",7,"h",8}} to result in res =res = {{DateObject[{2000,1,1}],"a",1,"b",2},{DateObject[{2000,1,1}],"c",3,"d",4},{DateObject[{2000,1,1}],"c",3,"d",5}, {DateObject[{1999,12,31}],"e",5,"f",6}, {"g",7,"h",8}} - so in this example there is match in the game between "a" and "b", a discrepancy in results in the match between "c" and "d" Sep 3 '18 at 23:55
• It works - thanks :) Sep 4 '18 at 2:52

SeedRandom[1]
pairs = Subsets[StringTemplate["team"] /@ Range[4] , {2}] ;
lisA = Flatten /@ Transpose[{RandomChoice[{"Saturday", "Sunday"}, 7],
Transpose /@ Transpose[{RandomChoice[pairs, 7], RandomInteger[3, {7, 2}] }] }];
lisA  // Grid // TeXForm


$\small\begin{array}{ccccc} \text{Sunday} & \text{team3} & 2 & \text{team4} & 0 \\ \text{Sunday} & \text{team1} & 1 & \text{team2} & 2 \\ \text{Saturday} & \text{team1} & 0 & \text{team2} & 0 \\ \text{Sunday} & \text{team2} & 3 & \text{team3} & 3 \\ \text{Saturday} & \text{team2} & 3 & \text{team4} & 1 \\ \text{Saturday} & \text{team1} & 1 & \text{team2} & 0 \\ \text{Saturday} & \text{team1} & 0 & \text{team2} & 1 \\ \end{array}$

lisB = Flatten/@Transpose/@Transpose[{RandomChoice[pairs, 10], RandomInteger[3, {10, 2}]}];
lisB // Grid // TeXForm


$\small\begin{array}{cccc} \text{team1} & 2 & \text{team4} & 0 \\ \text{team1} & 1 & \text{team2} & 3 \\ \text{team3} & 2 & \text{team4} & 1 \\ \text{team3} & 2 & \text{team4} & 0 \\ \text{team1} & 0 & \text{team2} & 0 \\ \text{team1} & 2 & \text{team3} & 1 \\ \text{team1} & 2 & \text{team2} & 1 \\ \text{team2} & 0 & \text{team4} & 2 \\ \text{team1} & 3 & \text{team4} & 1 \\ \text{team1} & 1 & \text{team4} & 1 \\ \end{array}$

lisB2 = DeleteCases[lisB, Alternatives @@ (Rest /@ lisA)]; (* from Bill's comment *)
lisB3 = Select[lisB2, And @@ (Function[x, #[[{1, 3}]] =!= x[[{1, 3}]]] /@
lisA[[All, 2 ;;]]) &];
lisB4 = DeleteDuplicates[Join @@ Outer[
If[#[[{1, 3}]] == #2[[{2, 4}]], Prepend[#, #2[[1]]], Nothing] &,
Complement[ lisB2, lisB3], lisA, 1]];
Join[lisA, lisB3, lisB4] // Grid //TeXForm


$\small\begin{array}{ccccc} \text{Sunday} & \text{team3} & 2 & \text{team4} & 0 \\ \text{Sunday} & \text{team1} & 1 & \text{team2} & 2 \\ \text{Saturday} & \text{team1} & 0 & \text{team2} & 0 \\ \text{Sunday} & \text{team2} & 3 & \text{team3} & 3 \\ \text{Saturday} & \text{team2} & 3 & \text{team4} & 1 \\ \text{Saturday} & \text{team1} & 1 & \text{team2} & 0 \\ \text{Saturday} & \text{team1} & 0 & \text{team2} & 1 \\ \text{team1} & 2 & \text{team4} & 0 & \text{} \\ \text{team1} & 2 & \text{team3} & 1 & \text{} \\ \text{team1} & 3 & \text{team4} & 1 & \text{} \\ \text{team1} & 1 & \text{team4} & 1 & \text{} \\ \text{Sunday} & \text{team1} & 1 & \text{team2} & 3 \\ \text{Saturday} & \text{team1} & 1 & \text{team2} & 3 \\ \text{Sunday} & \text{team1} & 2 & \text{team2} & 1 \\ \text{Saturday} & \text{team1} & 2 & \text{team2} & 1 \\ \text{Saturday} & \text{team2} & 0 & \text{team4} & 2 \\ \text{Sunday} & \text{team3} & 2 & \text{team4} & 1 \\ \end{array}$

• Defying the style manual, i'll dare to say thanks, kglr! Sep 4 '18 at 0:09
• @Suite401, my pleasure. Thank you for the accept.
– kglr
Sep 4 '18 at 0:19
• i'm going to ask a more specific version of my question Sep 4 '18 at 5:17