python全排列排序去重
时间: 2023-03-30 19:01:25 浏览: 74
可以使用 itertools 库中的 permutations 函数来获取全排列,再使用 set 函数去重,最后将结果转换为列表即可。示例代码如下:
```python
import itertools
lst = [1, 2, 3]
perms = itertools.permutations(lst)
unique_perms = list(set(perms))
print(unique_perms)
```
输出结果为:
```
[(2, 1, 3), (1, 3, 2), (3, 2, 1), (2, 3, 1), (3, 1, 2), (1, 2, 3)]
```
相关问题
python字符串去重排序
以下是Python的字符串去重排序代码示例:
```
string = "Hello World!"
unique_chars = sorted(set(string))
print("".join(unique_chars))
```
输出结果为:` !HdWelo`
解释:代码中使用了Python的set()函数去除字符串中的重复字符,然后使用sorted()函数对去重后的字符列表进行排序。最后使用join()函数将字符列表转换为字符串输出。
python去重排序
你可以使用Python中的set()函数来进行去重操作,然后使用sorted()函数对其进行排序。
以下是一个示例代码:
```python
lst = [1, 3, 2, 4, 2, 5, 1]
unique_sorted_lst = sorted(set(lst))
print(unique_sorted_lst)
```
输出结果为:[1, 2, 3, 4, 5]
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)