1

I have a list such as

x=[1,2,4,5,8,9]

What I wanted to implement is to get a list which contains combination of 4 digits out of the list. For example: Output should be

[1,2,4,5]
[2,3,4,8]
.....
[1,3,5,8]
....

Hence, I believe it will be 4^6 solutions. I have tried itertools combination with no success

Stefan
  • 11
  • 1
  • 6
  • Possible duplicate of [How do I generate permutations of length LEN given a list of N Items?](https://stackoverflow.com/questions/9338052/how-do-i-generate-permutations-of-length-len-given-a-list-of-n-items) – Bill the Lizard Feb 12 '19 at 18:57
  • What's your expected outcome though? It looks like you are either looking for `itertools.combinations` or `itertools.permutations` depending on whether you want repeats or not. – r.ook Feb 12 '19 at 18:59
  • Yes, I didn't looked in the `permutations` part. The answer below helped. – Stefan Feb 12 '19 at 19:06
  • Note that `permutations` gets you 360 results not `4^6` (I think you mean `4**6`) which would be 4096 results. – Steven Rumbalski Feb 12 '19 at 19:11
  • You probably have to search more in detail at Stackoverflow, your question has already been solved in the following link: This is a generalization for any k. https://stackoverflow.com/questions/7378180/generate-all-subsets-of-size-k-containing-k-elements-in-python – Gerardo Gonzalez - Nemis Feb 12 '19 at 19:01
  • Just to set the math straight here: The number of *permutations* will be `6!/(6-4)! = 360`, the number of *combinations* will be `6!/(4! * (6-4)!) = 15` (binomial coefficient) – ChrisB Apr 06 '23 at 11:39

3 Answers3

7

Itertools is actually the answer, because what you want are permutations of length N.

import itertools as it

x=[1,2,4,5,8,9]

print(list(it.permutations(x, 4)))
Mikael Brenner
  • 357
  • 1
  • 7
1

If order does not matter you should use itertools.combinations:

from itertools import combinations
x=[1,2,4,5,8,9]
for c in combinations(x, 4):
    print(c)

This outputs:

(1, 2, 4, 5)
(1, 2, 4, 8)
(1, 2, 4, 9)
(1, 2, 5, 8)
(1, 2, 5, 9)
(1, 2, 8, 9)
(1, 4, 5, 8)
(1, 4, 5, 9)
(1, 4, 8, 9)
(1, 5, 8, 9)
(2, 4, 5, 8)
(2, 4, 5, 9)
(2, 4, 8, 9)
(2, 5, 8, 9)
(4, 5, 8, 9)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • From the docs for `combinations()`: "if the input iterable is sorted, the combination tuples will be produced in sorted order." Pretty cool, in my opinion. (Of course there are other orderings, so maybe that's what you meant.) – Steven Rumbalski Feb 12 '19 at 18:59
  • 1
    Yes, but what I meant when I said "if order does not matter" is if sequences with the same elements but different orders count as the same, such as `(1, 2, 4, 5)` and `(1, 2, 5, 4)`. – blhsing Feb 12 '19 at 19:02
  • Ah. Now I know what you meant. I'll leave my comment so your clarification makes sense in case anyone else misreads it. – Steven Rumbalski Feb 12 '19 at 19:03
0

You can try this:

from itertools import combinations
x=[1,2,4,5,8,9]
comb = list(map(list, itertools.combinations(x, 4)))
print(comb)

[[1, 2, 4, 5], [1, 2, 4, 8], [1, 2, 4, 9], [1, 2, 5, 8], [1, 2, 5, 9], [1, 2, 8, 9], [1, 4, 5, 8], [1, 4, 5, 9], [1, 4, 8, 9], [1, 5, 8, 9], [2, 4, 5, 8], [2, 4, 5, 9], [2, 4, 8, 9], [2, 5, 8, 9], [4, 5, 8, 9]]
Kenan
  • 13,156
  • 8
  • 43
  • 50