4

Edit: I confused the difference between permutations and combinations. Edited the question just to keep it up (as I can't delete it). Though I realized my mistake.

I've been browsing this question around for a bit and I couldn't find a solution that fits uniquely to what I'm looking for.

The basic concept is if I ran a function gen_permutations([1,2]) to generate permutations. I would get the result ([1,2],[2,1]), though in this result, I would classify they are the same set.

I would think of checking something as result[0] < result[1] then add it to the set of results. Though, how would this be scaled up for example gen_permutations(input, size=n | n <= len(input))?

Sorry if there's a question answered to this, but all "Similar Questions" point me to just "unique results" where [1,2] != [2,1].

Edit: Case for confusion, the function gen_permutations is just a generic generator that I found and used here How to generate all permutations of a list in Python

Another example to make it bigger. I could run this case

for i in permutations([1,2,3,4],3):
    print i

Gets the result (1, 2, 3), (1, 2, 4),(1, 3, 2),(1, 3, 4),(1, 4, 2),(1, 4, 3),(2, 1, 3),(2, 1, 4),(2, 3, 1),(2, 3, 4),(2, 4, 1),(2, 4, 3),(3, 1, 2),(3, 1, 4),(3, 2, 1),(3, 2, 4),(3, 4, 1),(3, 4, 2),(4, 1, 2),(4, 1, 3),(4, 2, 1),(4, 2, 3),(4, 3, 1),(4, 3, 2)

Though (1,2,3) and (3,2,1) I would consider not unique. Since if you reordered them they would be the same set. I need help to code a generator of permutations that would not generate the result (3,2,1) as (1,2,3) was already generated.

Community
  • 1
  • 1
Nom
  • 189
  • 4
  • 14
  • 1
    Your question isn't quite clear: you want permutations but not all of them ? Which one do you exclude ? You could add more examples to clarify too. – jadsq Oct 22 '16 at 14:08
  • Your question is unclear. Do you have a function `gen_permutations()` or are you asking how to write one? – Gordon Linoff Oct 22 '16 at 14:10
  • A permutation is just an ordering of the elements in a set. The only output that makes sense for the function you are asking for is to wrap the input in other list: `get_permutations([1,2]) = [[1,2]]`. – chepner Oct 22 '16 at 14:14

2 Answers2

8

Are you looking for combinations? In maths, combinations don't care about order so [1,2] is equal to [2,1]. This article explains more.

In python you can simply do:

import itertools
list(itertools.combinations([1,2], 2))

Which gives the output:

[(1,2)]
ChrisD
  • 3,378
  • 3
  • 35
  • 40
  • 1
    An explanation of the difference between [combinations and permutations](https://betterexplained.com/articles/easy-permutations-and-combinations/). With combinations, order doesn't matter, which seems to be what you want here. – jjst Oct 22 '16 at 14:15
  • Yep exactly what I wanted. I completely forgot about the difference. I was too honed into "generate all possibilities". Thank you! – Nom Oct 22 '16 at 14:18
1

In permutations the order matters, so [1,2] is different than [2,1].

It seems that what you want are combinations, and python has buildin support for it.

import itertools
print list(itertools.combinations('ABCD', 2))

The result is:

[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]

You can also iterate through the results since itertools.combinations() returns an iterator.

import itertools
for i in itertools.combinations('ABCD', 2):
    print i

Result:

('A', 'B')
('A', 'C')
('A', 'D')
('B', 'C')
('B', 'D')
('C', 'D')
Thanassis
  • 604
  • 7
  • 16