Manipulation of string list

I have a list:

lis = {{"a", "b", "X", "0"}, {"a", "f", "z", "1"}, {"g", "a", "z", "1"}, {"a", "b", "x", "0"}};


The last field of each element of lis is either "0" or "1".

I need to convert the third field in each element to upper case and then determine if there are any two elements in the list which have now become duplicates, delete the duplicate, and then change the last field of the remaining element from the deletion pair from "0" to "1" to get:

res = {{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}


Making the third field upper case and deleting duplicates is simple, but I'm not sure how to convert the last field from "0" to "1". Thanks for any suggestions...

• It seems that DeleteDuplicates is useful here: DeleteDuplicates[MapAt[ToUpperCase, lis, {All, 3}]] /. "0" -> "1" Jan 29, 2020 at 8:49
• @ΑλέξανδροςΖεγγ this would break if one of the non-duplicates elements has "0" in its 4th index Jan 29, 2020 at 8:50

First /@
(GatherBy[MapAt[ToUpperCase, lis, {;;, 3}], Most] /. {{a__, "0"|"1"}, __} :> {{a, "1"}})


{{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}

Also

Tally[MapAt[ToUpperCase, lis, {;; , 3}], Most[#] == Most[#2] &] /.
{{a__, b_}, c_} :> {a, c /. {1 -> b, _ -> "1"}}


{{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}

and

SequenceReplace[Sort @ MapAt[ToUpperCase, lis, {;; , 3}],
{Repeated[{a__, _}, {2, ∞}]} :> {a, "1"}, ∞]


{{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}

This should also work:

#[[1]] /. {a_, b_, c_, _} :> {a, b, c, "1"} /; #[[2]] > 1 & /@
Normal[Counts[MapAt[ToUpperCase, lis, {All, 3}]]]


Using SubsetMap:

lis = {{"a", "b", "X", "0"}, {"a", "f", "z", "1"}, {"g", "a", "z",
"1"}, {"a", "b", "x", "0"}};

GatherBy[lis, {#[[1]], #[[2]], ToLowerCase[#[[3]]]} &] //
Replace[#, {a_List, b__List} :> {Most@a~Join~{"1"}}, 1] & //
Map[First] // SubsetMap[ToUpperCase, #, {All, 3}] &


or

(lis // Replace[#, {a_, b_, c_, d__} :> {a, b, ToUpperCase[c], d},
1] & // GatherBy[#, Most] &) /. {{a_?VectorQ} :> a,
a_?MatrixQ :> {Splice@ a[[1, 1 ;; -2]], "1"}


Result:

{{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}

• I think the 0 in the first subset must be 1 (like @kglr)
– eldo
Sep 15, 2023 at 16:49
• I have updated the answer, @eldo.
– Syed
Sep 15, 2023 at 17:12

Using GroupBy for your last condition:

f1[{a_, b_, c_, d_}] := {a, b, c, "1"};

f2 = Union @@ Values[GroupBy[#, Last,
f1 /@ MapAt[ToUpperCase, #, {All, 3}] &]] &;


Test:

lis = {{"a", "b", "X", "0"}, {"a", "f", "z", "1"},
{"g", "a", "z", "1"}, {"a", "b", "x", "0"}};

res = {{"a", "b", "X", "1"}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}};

f2@lis === res

(*True*)


Or using SortBy and SplitBy:

f3 = Union @@ Map[f1, SplitBy[
SortBy[MapAt[ToUpperCase, #, {All, 3}], Last],
Last], {2}] &;

f3@lis === res

(*True*)

list =
{{"a", "b", "X", "0"}, {"a", "f", "z", "1"},
{"g", "a", "z", "1"}, {"a", "b", "x", "0"}};


Using Counts

With[{c = Counts @ MapAt[ToUpperCase, list, {All, 3}]},
Join[
MapAt[1&, {All, -1}] @ Keys @ Select[UnequalTo @ 1] @ c,
Keys @ Select[EqualTo @ 1] @ c]]
`

{{"a", "b", "X", 1}, {"a", "f", "Z", "1"}, {"g", "a", "Z", "1"}}