14

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?

rm -rf
  • 88,781
  • 21
  • 293
  • 472
Red Banana
  • 5,329
  • 2
  • 29
  • 47

4 Answers4

19

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.

acl
  • 19,834
  • 3
  • 66
  • 91
  • 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? – Red Banana Mar 19 '12 at 20:12
  • does this http://reference.wolfram.com/mathematica/ref/Slot.html help? – acl Mar 19 '12 at 20:47
  • 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] &] – Red Banana Mar 19 '12 at 22:29
  • does my added explanation in the answer help? or the linked question? If not, try asking a more specific question – acl Mar 19 '12 at 22:31
11

You could try this as an alternative:

Sort /@ Permutations[Range@16, {2}] // Union
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
image_doctor
  • 10,234
  • 23
  • 40
  • Union considers {1,2} = {2,1}, isn't it? I didn't remember of using basic math tools. – Red Banana Mar 19 '12 at 20:18
  • What's the function of Sort/@? I've looked at this page [link] (http://reference.wolfram.com/mathematica/ref/Sort.html?q=Sort&lang=en) 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 – Red Banana Mar 19 '12 at 22:35
  • 1
    Union[{{2, 1}, {1, 2}}] returns {{2,1},{1,2}} so it seems for Union[] order is important. Perhaps not what you would expect. – image_doctor Mar 20 '12 at 00:28
  • 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. – image_doctor Mar 20 '12 at 00:32
  • The Map function is fundamental to functional programming and a very useful tool to have at your disposal. It removes the need for loop counters etc that plague procedural programming and just applies any function to each element in a list. Functional programs often turn out to need a lot less code and avoid many of the bugs present in other styles of coding, particularly those associated with variables holding execution state which sometimes lose coherency. Hope that helps. – image_doctor Mar 20 '12 at 00:37
  • As you would like {2,1} and {1,2} to be considered the same, i.e. order does not matter, technically what you want are combinations not permutations. Which is why some post processing is need on the output of Permutations. – image_doctor Mar 20 '12 at 00:40
  • 3
    @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! – CHM Mar 20 '12 at 04:01
9

@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]
DavidC
  • 16,724
  • 1
  • 42
  • 94
9

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

Taliesin Beynon
  • 10,639
  • 44
  • 51