python某值列表去重
时间: 2024-01-27 19:15:44 浏览: 56
python列表去重的二种方法
以下是使用Python对某值列表进行去重的方法:
1. 使用集合(set)去重:
```python
original_list = [3, 7, 4, 4, 3, 2, 1]
my_set = set(original_list)
result_list = list(my_set)
print(result_list) # 输出:[1, 2, 3, 4, 7]
```
2. 使用列表推导式去重:
```python
original_list = [3, 7, 4, 4, 3, 2, 1]
result_list = list(dict.fromkeys(original_list))
print(result_list) # 输出:[3, 7, 4, 2, 1]
```
3. 使用循环遍历去重:
```python
original_list = [3, 7, 4, 4, 3, 2, 1]
result_list = []
for num in original_list:
if num not in result_list:
result_list.append(num)
print(result_list) # 输出:[3, 7, 4, 2, 1]
```
阅读全文