I've written the following code to print all the permutations of a list. For example:
\$[1, 2, 3]\$ has these permutations: \$[1,2,3]\$, \$[1,3,2]\$, \$[2,1,3]\$, \$[2,3,1]\$, \$[3,1,2]\$, and \$[3,2,1]\$.
def insert_each_position(self, nums, insert_one):
res, len_of_nums = [], len(nums)
for idx in range(len_of_nums+1):
tmp = nums[:]
tmp.insert(idx, insert_one)
res.append(tmp)
return res
def permute(self, nums):
res, len_of_nums = [[]], len(nums)
for idx in range(len_of_nums):
cur_num = nums[idx]
tmp = []
for sub in res:
tmp.extend(self.insert_each_position(sub, cur_num))
res = tmp
return res
itertools.permutations
? docs.python.org/2/library/itertools.html#itertools.permutations \$\endgroup\$