Below, list
is a representative sample of my list, which contains lists of integers. I would like to be able to input:
list = {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}};
f[list]
and obtain the output:
{{1, 2, 3}, {2, 1, 3}}
In other words, {3, 2, 1}
is considered to be the same as {1, 2, 3}
, since in reverse it is exactly {1, 2, 3}
. However, {2, 1, 3}
is not considered to be the same as either {1, 2, 3}
or {3, 2, 1}
, because it does not match these lists in forward or in reverse.
What function f
can I use to accomplish this?
I tried this:
DeleteDuplicates[list, MemberQ[list, #] || MemberQ[Reverse /@ list, #] &]
but it does not work, although I'm not sure why.
ADDENDUM
Now suppose I want to input:
list = {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}, {1, 2, 3}};
and obtain:
list = {{1, 2, 3}, {2, 1, 3}};
where the "second" {1, 2, 3}
is removed as a "normal" duplicate. How can I do this? I could do:
DeleteDuplicates[DeleteDuplicates[list, (#1 == Reverse[#2] &)]]
but is there an easier way?
Answer
This should do the job:
DeleteDuplicates[list, SameQ[#1, #2] || SameQ[#1, Reverse@#2] &]
Comments
Post a Comment