Skip to main content
added 228 characters in body
Source Link
Mark McClure
  • 32.6k
  • 3
  • 103
  • 163
subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;Sort[set1];
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

Seems to work even ifIf the elementslists are stringsnot ordered, however, then my subsetQ wasn't working; should take order into account. This might fix it:

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;Sort[set1];
lists = Map[ToString{{"test", {"1"}, {a"test", b"1", c"1"}, {a"test", b"1", d"2"}, 
  {d"test", e"1", "3"}, {d"test", "1", "4"}, {a"test", "1", "5"}, 
  {a"test", b"2"}, {f"test", "2", "1"}, {"test", "2", "2"}, 
  {2"test", "2", "3"}];};
Select[lists, !Or@@Table[subsetQ[# Or @@ Table[subsetQ[#, set], {set, 
  Complement[lists, {#}]}] &] // InputForm

(* Out: {{"a""test", "b""1", "c""1"}, {"a""test", "b""1", "d""3"}, {"d""test", "e""1", "4"}, 
         {"f""test", "1", "5"}, {"test", "2", "2"}, {"test", "2", "3"}} *)

I still think we might get tens of thousands of views and hundreds of upvotes! I mean, this is an actual application.Is that what you're hoping for?

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

Seems to work even if the elements are strings:

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = Map[ToString, {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}}, {2}];
Select[lists, !Or@@Table[subsetQ[#, set], {set, 
  Complement[lists, {#}]}] &] // InputForm

(* Out: {{"a", "b", "c"}, {"a", "b", "d"}, {"d", "e"}, {"f"}} *)

I still think we might get tens of thousands of views and hundreds of upvotes! I mean, this is an actual application.

subsetQ[set1_, set2_] := Intersection[set1, set2] == Sort[set1];
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

If the lists are not ordered, however, then my subsetQ wasn't working; should take order into account. This might fix it:

subsetQ[set1_, set2_] := Intersection[set1, set2] == Sort[set1];
lists = {{"test", "1"}, {"test", "1", "1"}, {"test", "1", "2"}, 
  {"test", "1", "3"}, {"test", "1", "4"}, {"test", "1", "5"}, 
  {"test", "2"}, {"test", "2", "1"}, {"test", "2", "2"}, 
  {"test", "2", "3"}};
Select[lists, ! Or @@ Table[subsetQ[#, set], {set, 
  Complement[lists, {#}]}] &] // InputForm

(* Out: {{"test", "1", "1"}, {"test", "1", "3"}, {"test", "1", "4"}, 
         {"test", "1", "5"}, {"test", "2", "2"}, {"test", "2", "3"}} *)

Is that what you're hoping for?

added 904 characters in body
Source Link
Mark McClure
  • 32.6k
  • 3
  • 103
  • 163

How about

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

I wonder what our chancesLet's pick this apart. Select[list, f] selects from list all those elements x for which f[x] is True. Now the Table[..] portion takes one of gettingthe elements (represented by #) of the lists and checks to see if it is a subet of each of the other lists. Of course, we don't want to check # itself, hence the Complement[..] business. Here's an illustration where #={d}:

Complement[lists, {{d}}]
dTest = Table[subsetQ[{d}, set], {set, Complement[lists, {{d}}]}]

(* Out1: {{a}, {f}, {a, b}, {d, e}, {a, b, c}, {a, b, d}} *)
(* Out2: {False, False, False, True, False, True} *)

Now, if anyone of these is True, we want to exclude that list. Note that Or@@dTest returns True if any elment of the list dTest is True.

Or @@ dTest

(* Out: True *)

Slap the negator ! in front, and we see that {d} is excluded.

Seems to work even if the elements are strings:

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = Map[ToString, {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}}, {2}];
Select[lists, !Or@@Table[subsetQ[#, set], {set, 
  Complement[lists, {#}]}] &] // InputForm

(* Out: {{"a", "b", "c"}, {"a", "b", "d"}, {"d", "e"}, {"f"}} *)

I still think we might get tens of thousands of views and hundreds of upvotes on! I mean, this are?is an actual application.

How about

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

I wonder what our chances of getting tens of thousands of views and hundreds of upvotes on this are?

How about

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

Let's pick this apart. Select[list, f] selects from list all those elements x for which f[x] is True. Now the Table[..] portion takes one of the elements (represented by #) of the lists and checks to see if it is a subet of each of the other lists. Of course, we don't want to check # itself, hence the Complement[..] business. Here's an illustration where #={d}:

Complement[lists, {{d}}]
dTest = Table[subsetQ[{d}, set], {set, Complement[lists, {{d}}]}]

(* Out1: {{a}, {f}, {a, b}, {d, e}, {a, b, c}, {a, b, d}} *)
(* Out2: {False, False, False, True, False, True} *)

Now, if anyone of these is True, we want to exclude that list. Note that Or@@dTest returns True if any elment of the list dTest is True.

Or @@ dTest

(* Out: True *)

Slap the negator ! in front, and we see that {d} is excluded.

Seems to work even if the elements are strings:

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = Map[ToString, {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}}, {2}];
Select[lists, !Or@@Table[subsetQ[#, set], {set, 
  Complement[lists, {#}]}] &] // InputForm

(* Out: {{"a", "b", "c"}, {"a", "b", "d"}, {"d", "e"}, {"f"}} *)

I still think we might get tens of thousands of views and hundreds of upvotes! I mean, this is an actual application.

Source Link
Mark McClure
  • 32.6k
  • 3
  • 103
  • 163

How about

subsetQ[set1_, set2_] := Intersection[set1, set2] == set1;
lists = {{a, b, c}, {a, b, d}, {d, e}, {d}, {a}, {a, b}, {f}};
Select[lists, ! Or @@ Table[subsetQ[#, set],
 {set, Complement[lists, {#}]}] &]

I wonder what our chances of getting tens of thousands of views and hundreds of upvotes on this are?