1
$\begingroup$

Possible Duplicate:
How can I remove B -> A from a list if A -> B is in the list?
Delete duplicate elements from list
Pattern matching deletion of list items

How do I delete the duplicates of a set who's elements are themselves sets?

Here is an example: Deleting the duplicates from {{x,y},{y,x}} should give {{x,y}} (or {{y,x}}, both answers are equivalent).

I am particularly interested in the case when x and y are lists, but a solution that works for x and y of any type would be better.

$\endgroup$
8
  • $\begingroup$ Actually, the title should be: "Operations on lists of sets", as your example is not a set (though the outer list does contain sets). $\endgroup$ Jun 29, 2012 at 19:37
  • $\begingroup$ @István The top and second level data structures have the same data type, so I think it should either be "...lists of lists" or "...sets of sets". I picked the latter to emphasize that I am thinking of them as sets even though the actual data type in Mathematica is a list. $\endgroup$ Jun 29, 2012 at 19:43
  • $\begingroup$ It is not clear for me what are you expecting from deleting duplicates from {{2, 1}, {1, 2}, {3, 1}}. Should it be {{1, 2}, {1, 3}} or {1, 2, 3}? $\endgroup$ Jun 29, 2012 at 19:44
  • 1
    $\begingroup$ This seems to be a duplicate of Pattern matching deletion of list items which itself was a duplicate of How can I remove B -> A from a list if A -> B is in the list. $\endgroup$
    – Jens
    Jun 29, 2012 at 20:34
  • 1
    $\begingroup$ This might answer your question, too: How to use Union on list of lists without sorting $\endgroup$ Jun 29, 2012 at 21:26

2 Answers 2

5
$\begingroup$

Does

DeleteDuplicates[Sort /@ {{2, 1}, {1, 2}}]

help?

Edit : ( Just try your data - Mathematica is cool, isn't it ? )

DeleteDuplicates[Sort /@ {{x, y}, {y, x}}]
$\endgroup$
5
  • $\begingroup$ Sorry for the misleading example. I clarified the question and example to make it clear that the elements of the inner sets are not integers. $\endgroup$ Jun 29, 2012 at 19:36
  • $\begingroup$ Just try it with your data. $\endgroup$
    – user21
    Jun 29, 2012 at 19:45
  • $\begingroup$ Weird, I had tried it, but I thought it gave me the wrong answer. It is working now though, so thanks! $\endgroup$ Jun 29, 2012 at 19:47
  • $\begingroup$ I see. I think the order of operations tricked me and it executed the Sort /@ part sooner than I wanted. Adding parentheses to force the correct order fixed that. $\endgroup$ Jun 29, 2012 at 19:50
  • $\begingroup$ Glad you could figure it out. $\endgroup$
    – user21
    Jun 29, 2012 at 19:51
1
$\begingroup$

Here is another solution based on Union and its option SameTest :

Union[{{x, y}, {y, x}}, SameTest -> (Union[#1] == Union[#2] &)]
{{x, y}}
Union[{{2, 1}, {1, 2}, {3, 1}}, SameTest -> (Union[#1] == Union[#2] &)]
{{1, 2}, {3, 1}}

In case of more nested lists I would use Flatten on appropriate level, e.g.

Union @ Flatten[{ {{2, 1}}, {{1, 2}} }, 2]
{1, 2}

where DeleteDuplicates[Sort /@ { {{2, 1}}, {{1, 2}} }] does not work if we don't use Flatten. However, this is not a general solution, one has to consider appropriate examples and work on case by case basis.

$\endgroup$
5
  • $\begingroup$ Sorry for the misleading example. I clarified the question and example to make it clear that the elements of the inner sets are not integers. $\endgroup$ Jun 29, 2012 at 19:38
  • 1
    $\begingroup$ Pardon me but this does not delete duplicate sets: deleting duplicates from {{2, 1}, {1, 2}, {3, 1}} should result in {{1,2}, {1,3}}, as those are the two distinct sets, but Union@Flatten[{{2, 1}, {1, 2}, {3, 1}}] returns {1, 2, 3}. $\endgroup$ Jun 29, 2012 at 19:41
  • $\begingroup$ @TysonWilliams Does it satisfy your needs ? Otherwise, could you give an appropriate example ? $\endgroup$
    – Artes
    Jun 29, 2012 at 21:18
  • $\begingroup$ @IstvánZachar Thanks for reporting the problem. I updated the answer. If you see another problems, let me know about them. Nethertheless, I hope it is a good alternative to ruebenko's answer, which is not too general, either . $\endgroup$
    – Artes
    Jun 29, 2012 at 21:21
  • $\begingroup$ @Artes: Nice use of SameTest! $\endgroup$ Jun 29, 2012 at 21:39

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