14
$\begingroup$

I want to get the Permutations on the elements of a list. Then I'm doing this:

a = Drop[Permutations[Range[1, 16], 2], {1, 17}]
i = 15;
While[i <= (240 - 16), a = Drop[a, {i, i}]; i = (i + (16 - 1))]

The first line of the code gives me all the permutations, but I do not want repetitions, like: I already have {1,2}, I do not want the {2,1}.

I'm trying to do this way but there's no result yet.

Any help?

$\endgroup$
2
  • 2
    $\begingroup$ I am still confused about how what you want differs from Subsets[list, {2}]. $\endgroup$
    – Mr.Wizard
    Commented Mar 20, 2012 at 17:05
  • $\begingroup$ Possible duplicates: (44), (1302) $\endgroup$
    – Mr.Wizard
    Commented Jan 29, 2016 at 23:18

4 Answers 4

19
$\begingroup$

Did I understand correctly?

Subsets[Range[1, 16], {2}]

EDIT: If you want to use Permutations, you could use

DeleteDuplicates[Permutations[Range[16], {2}], Sort[#1] == Sort[#2] &]

which deletes all "duplicates", where "duplicate" is defined by the equality of the two lists when sorted (ie, {2,3} is "equal" to {3,2} for purposes of this comparison).

EDIT: The meaning of #1 and #2 may be demonstrated by this example:

f={#1,#2}&

and then f[a,b] evaluates to {a,b}. That is, you are defining a pure function which takes two arguments, returning a list containing the two arguments, and assigning it to f. This could also be useful.

In the DeleteDuplicates example above, I am using as a test function (see second usage example in the documentation and also this example) that considers two lists equal if they are the same after sorting; thus, {3,4} is equal to {4,3}, since when sorted they both become {3,4}.

See also this.

$\endgroup$
4
  • $\begingroup$ What's the meaning of the #'s? I know # is a slot and it have something to do with patterns, but I never undertood how to use them. Any reference? $\endgroup$
    – Red Banana
    Commented Mar 19, 2012 at 20:12
  • $\begingroup$ does this reference.wolfram.com/mathematica/ref/Slot.html help? $\endgroup$
    – acl
    Commented Mar 19, 2012 at 20:47
  • $\begingroup$ It's the same of the Mathematica help. It's strange because there's a simple description on the slot thing, but it's always used on a bigger scope. Like.... With that information, i couldn't produce something like your code: DeleteDuplicates[Permutations[Range[16], {2}], Sort[#1] == Sort[#2] &] $\endgroup$
    – Red Banana
    Commented Mar 19, 2012 at 22:29
  • $\begingroup$ does my added explanation in the answer help? or the linked question? If not, try asking a more specific question $\endgroup$
    – acl
    Commented Mar 19, 2012 at 22:31
11
$\begingroup$

You could try this as an alternative:

Sort /@ Permutations[Range@16, {2}] // Union
$\endgroup$
7
  • $\begingroup$ Union considers {1,2} = {2,1}, isn't it? I didn't remember of using basic math tools. $\endgroup$
    – Red Banana
    Commented Mar 19, 2012 at 20:18
  • $\begingroup$ What's the function of Sort/@? I've looked at this page [link] (reference.wolfram.com/mathematica/ref/…) and i know it orders elements on a list but the elements of my list are already ordered and simply doing Union@a wont work, only Sort /@ a // Union $\endgroup$
    – Red Banana
    Commented Mar 19, 2012 at 22:35
  • 1
    $\begingroup$ Union[{{2, 1}, {1, 2}}] returns {{2,1},{1,2}} so it seems for Union[] order is important. Perhaps not what you would expect. $\endgroup$ Commented Mar 20, 2012 at 0:28
  • $\begingroup$ The expression /@ maps the function Sort[] over all the values returned by the call to Permutations. The effect is to sort each permutation into numerical order. This makes {2,1} into {1,2} so that it matches {1,2} when Union[] is applied and the duplicates are then removed. $\endgroup$ Commented Mar 20, 2012 at 0:32
  • 3
    $\begingroup$ @image_doctor The information provided in your comments could as well be written in the main answer. Actually, I think it should be part of your answer! $\endgroup$
    – CHM
    Commented Mar 20, 2012 at 4:01
9
$\begingroup$

@acl showed the best way. Because your list is defined as a Range, the particular solution for the case you gave is also found by

Table[{j, k}, {j, 15}, {k, j + 1, 16}]~Flatten~1

EDIT: Here's another way to solve it via Permutations:

Select[Permutations[Range@16, {2}], #[[2]] > #[[1]] &]

As TomD noted, this can be expressed alternatively as:

Select[Permutations[Range@16, {2}], OrderedQ]
$\endgroup$
2
  • 2
    $\begingroup$ Or, as a slight variation: Select[Permutations[Range@16, {2}], OrderedQ] $\endgroup$
    – user1066
    Commented Mar 20, 2012 at 4:53
  • 1
    $\begingroup$ @TomD Nice idea: OrderedQ as a selection criterion. $\endgroup$
    – DavidC
    Commented Mar 20, 2012 at 7:43
9
$\begingroup$

In addition to the other approaches that have already been given, you can use DeleteDuplicatesBy[list, Sort], which is new in 10.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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