Created
November 16, 2011 16:36
-
-
Save rioleo/1370583 to your computer and use it in GitHub Desktop.
lexicographic-permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def permutations(li): | |
""" Return all permutations of a given list. This function | |
assumes every element of the list is unique. """ | |
if len(li) <= 1: | |
yield li | |
else: | |
for el in li: | |
for p in permutations([e for e in li if e != el]): | |
yield [el] + p | |
x = permutations([1, 2, 3, 4, 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment