生成一段全排序的python代码
时间: 2023-10-12 21:21:12 浏览: 96
Python CategoricalDtype自定义排序实现原理解析
以下是生成全排序的Python代码:
```python
def permute(nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums]
result = []
for i in range(len(nums)):
n = nums[i]
rem = nums[:i] + nums[i+1:]
for p in permute(rem):
result.append([n] + p)
return result
# 示例
print(permute([1, 2, 3]))
```
输出结果为:
```
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
```
阅读全文