3

What I would like to do is the following.

For a given list of elements; say (0,1,2,3,4) I would like to obtain all possible combinations of five, but not the ones with ANY repeated values. That is I don't want to see anything with two elements equal like (0,0,1,2,3), (0,1,2,3,0), etc etc.

Tuples gives all possible combinations back

list = {0, 1, 2, 3, 4};
Tuples[list, 5]

I found this answer Tuples Without Replacements

But this is not I want to do.

I also tried to use Subsets and Reverse

Subsets[list, {5}]
Reverse /@ Subsets[list, {5}]

But the result of the above is to give only the $(0,1,2,3,4)$ and $(4,3,2,1,0)$.

I checked a couple of other replies here and I have not been able to find anything relevant to get me started.

Thanks in advance.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355

4 Answers4

7

Just use Permutations:

Permutations[lst]

{{a, b, s, t}, {a, b, t, s}, {a, s, b, t}, {a, s, t, b}, {a, t, b, s}, {a, t, s, b}, {b, a, s, t}, {b, a, t, s}, {b, s, a, t}, {b, s, t, a}, {b, t, a, s}, {b, t, s, a}, {s, a, b, t}, {s, a, t, b}, {s, b, a, t}, {s, b, t, a}, {s, t, a, b}, {s, t, b, a}, {t, a, b, s}, {t, a, s, b}, {t, b, a, s}, {t, b, s, a}, {t, s, a, b}, {t, s, b, a}}

If you want to restrict to 3-element sublists as in @Anxon's answer use the second argument of Permutations:

Permutations[lst], {3}]

{{a, b, s}, {a, b, t}, {a, s, b}, {a, s, t}, {a, t, b}, {a, t, s}, {b, a, s}, {b, a, t}, {b, s, a}, {b, s, t}, {b, t, a}, {b, t, s}, {s, a, b}, {s, a, t}, {s, b, a}, {s, b, t}, {s, t, a}, {s, t, b}, {t, a, b}, {t, a, s}, {t, b, a}, {t, b, s}, {t, s, a}, {t, s, b}}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2
lst = {a, b, s, t}

tlst = Tuples[lst, 3]

srtlst = Cases[tlst, Except[ {___, x_, ___, x_, ___}]]
Anxon Pués
  • 907
  • 5
  • 13
1
list = Tuples[{a, b, s, t}, 3];

Using SequenceSplit (new in 11.3) with OrderlessPatternSequence

Join @@ SequenceSplit[list, {{OrderlessPatternSequence[a_, a_, _]}}]

{{a, b, s}, {a, b, t}, {a, s, b}, {a, s, t}, {a, t, b}, {a, t, s}, {b, a, s}, {b, a, t}, {b, s, a}, {b, s, t}, {b, t, a}, {b, t, s}, {s, a, b}, {s, a, t}, {s, b, a}, {s, b, t}, {s, t, a}, {s, t, b}, {t, a, b}, {t, a, s}, {t, b, a}, {t, b, s}, {t, s, a}, {t, s, b}}

Using DeleteCases with OrderlessPatternSequence

DeleteCases[{OrderlessPatternSequence[a_, a_, _]}] @ list

(* same result *)

Timings with Tuples[Range[20], 3] (8000 elements)

Permutations (Carl Woll): 0.00015 sec

Cases (Anxon Pués): 0.011 sec

DeleteCases (Eldo): 0.016 sec

SequenceSplit (Eldo): 1.019 sec

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 1
    It's nice to see these newer functions get advertised on MSE. But bear in mind that some usages might be unreasonably slow. And for this problem, the method shown by @CarlWoll is The Way To Do It. – Daniel Lichtblau Jan 27 '24 at 22:40
  • Thank you, Daniel, I included the timings. Especially SequenceSplit gets horribly slow with longer lists. – eldo Jan 28 '24 at 08:12
1
list = Tuples[{a, b, s, t}, 3];

Using Select and CountDistinct:

Select[list, CountDistinct[#] == 3 &]
{{a, b, s}, {a, b, t}, {a, s, b}, {a, s, t}, {a, t, b}, {a, t, s}, 
 {b, a, s}, {b, a, t}, {b, s, a}, {b, s, t}, {b, t, a}, {b, t, s},
 {s, a, b}, {s, a, t}, {s, b, a}, {s, b, t}, {s, t, a}, {s, t, b}, 
 {t, a,b}, {t, a, s}, {t, b, a}, {t, b, s}, {t, s, a}, {t, s, b}}
E. Chan-López
  • 23,117
  • 3
  • 21
  • 44