code_junkie

How to generate all permutations of a list in Python

Nov 11th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. permutations ([])
  2. []
  3.  
  4. permutations ([1,])
  5. [1]
  6.  
  7. permutations ([1,2])
  8. [1, 2]
  9. [2, 1]
  10.  
  11. permutations ([1,2,3])
  12. [1, 2, 3]
  13. [1, 3, 2]
  14. [2, 1, 3]
  15. [2, 3, 1]
  16. [3, 1, 2]
  17. [3, 2, 1]
  18.  
  19. import itertools
  20. itertools.permutations([1,2,3])
  21.  
  22. def all_perms(str):
  23. if len(str) <=1:
  24. yield str
  25. else:
  26. for perm in all_perms(str[1:]):
  27. for i in range(len(perm)+1):
  28. #nb str[0:1] works in both string and list contexts
  29. yield perm[:i] + str[0:1] + perm[i:]
  30.  
  31. import itertools
  32. itertools.permutations([1,2,3])
  33.  
  34. import itertools
  35.  
  36. print list(itertools.permutations([1,2,3,4], 2))
  37. [(1, 2), (1, 3), (1, 4),
  38. (2, 1), (2, 3), (2, 4),
  39. (3, 1), (3, 2), (3, 4),
  40. (4, 1), (4, 2), (4, 3)]
  41.  
  42. print list(itertools.combinations('123', 2))
  43. [('1', '2'), ('1', '3'), ('2', '3')]
  44.  
  45. print list(itertools.product([1,2,3], [4,5,6]))
  46. [(1, 4), (1, 5), (1, 6),
  47. (2, 4), (2, 5), (2, 6),
  48. (3, 4), (3, 5), (3, 6)]
  49.  
  50. print list(itertools.product([1,2], repeat=3))
  51. [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
  52. (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
  53.  
  54. myset = set((1,2,3))
  55. print myset
  56. set([1, 2, 3])
  57. myiterable = tuple(myset)
  58. print myiterable
  59. (1, 2, 3)
  60.  
  61. def permutations (orig_list):
  62. if not isinstance(orig_list, list):
  63. orig_list = list(orig_list)
  64.  
  65. yield orig_list
  66.  
  67. if len(orig_list) == 1:
  68. return
  69.  
  70. for n in sorted(orig_list):
  71. new_list = orig_list[:]
  72. pos = new_list.index(n)
  73. del(new_list[pos])
  74. new_list.insert(0, n)
  75. for resto in permutations(new_list[1:]):
  76. if new_list[:1] + resto <> orig_list:
  77. yield new_list[:1] + resto
  78.  
  79. def permute_in_place(a):
  80. a.sort()
  81. yield list(a)
  82.  
  83. if len(a) <= 1:
  84. return
  85.  
  86. first = 0
  87. last = len(a)
  88. while 1:
  89. i = last - 1
  90.  
  91. while 1:
  92. i = i - 1
  93. if a[i] < a[i+1]:
  94. j = last - 1
  95. while not (a[i] < a[j]):
  96. j = j - 1
  97. a[i], a[j] = a[j], a[i] # swap the values
  98. r = a[i+1:last]
  99. r.reverse()
  100. a[i+1:last] = r
  101. yield list(a)
  102. break
  103. if i == first:
  104. a.reverse()
  105. return
  106.  
  107. if __name__ == '__main__':
  108. for n in range(5):
  109. for a in permute_in_place(range(1, n+1)):
  110. print a
  111. print
  112.  
  113. for a in permute_in_place([0, 0, 1, 1, 1]):
  114. print a
  115. print
  116.  
  117. def permutations(head, tail=''):
  118. if len(head) == 0: print tail
  119. else:
  120. for i in range(len(head)):
  121. permutations(head[0:i] + head[i+1:], tail+head[i])
  122.  
  123. permutations('abc')
  124.  
  125. def permutList(l):
  126. if not l:
  127. return [[]]
  128. res = []
  129. for e in l:
  130. temp = l[:]
  131. temp.remove(e)
  132. res.extend([[e] + r for r in permutList(temp)])
  133.  
  134. return res
  135.  
  136. list2Perm = [1, 2.0, 'three']
  137. listPerm = [[a, b, c]
  138. for a in list2Perm
  139. for b in list2Perm
  140. for c in list2Perm
  141. if ( a != b and b != c and a != c )
  142. ]
  143. print listPerm
  144.  
  145. [
  146. [1, 2.0, 'three'],
  147. [1, 'three', 2.0],
  148. [2.0, 1, 'three'],
  149. [2.0, 'three', 1],
  150. ['three', 1, 2.0],
  151. ['three', 2.0, 1]
  152. ]
Add Comment
Please, Sign In to add comment