python 去重
时间: 2023-11-15 15:00:01 浏览: 95
Python中有多种方法可以对列表和数据框进行去重操作。常用的方法包括:
1. 对列表去重:
- 使用循环查找的方式
- 使用set()函数
- 使用列表推导式
- 使用filter()函数
- 使用keys()方式
2. 对数据框去重:
- 使用unique()对单属性列去重
- 使用drop_duplicates()对单属性列去重
- 使用drop_duplicates()对多属性列去重
- 使用duplicated()对多属性列去重
其中,使用set()函数和列表推导式是最常用的方法之一。使用set()函数可以将列表转换为集合,从而去除重复元素。使用列表推导式可以通过遍历列表的方式,将不重复的元素添加到新列表中。
对于数据框的去重操作,可以使用unique()函数对单属性列进行去重,也可以使用drop_duplicates()函数对单属性列和多属性列进行去重。使用duplicated()函数可以返回一个布尔型的Series,表示每行是否是重复行。
相关问题
python去重脚本
以下是一个Python的去重脚本的示例代码:
```python
import os
import glob
import filecmp
dir_path = r'D:\文件去重'
file_list = \[\]
count = 0
for i in glob.glob(dir_path + '/**/*', recursive=True):
if os.path.isfile(i):
file_list.append(i)
for x in file_list:
for y in file_list:
if x != y and os.path.exists(x) and os.path.exists(y):
if filecmp.cmp(x, y):
os.remove(y)
count += 1
print(f"运行结束,一共删除了{count}个重复文件")
```
这个脚本可以用于清除文件夹中的重复文件。它使用了os模块来与操作系统进行交互,glob模块用于查找文件目录和文件,并将搜索结果返回到一个列表中,filecmp模块用于比较文件的内容。脚本首先将文件添加到一个列表中,然后使用嵌套循环比较列表中的文件,如果发现重复的文件,则删除其中一个。最后,脚本会输出删除的重复文件的数量。
#### 引用[.reference_title]
- *1* [python 图片去重脚本](https://blog.csdn.net/HaoZiHuang/article/details/127756170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Python 实用脚本 实现文件重复去重](https://blog.csdn.net/Abieai/article/details/128836571)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
python去重函数
Python中有多种方法可以实现列表去重的功能。以下是其中几种常用的方法:
1. 使用set()函数:将列表转换为集合,由于集合的元素具有唯一性,重复元素会被自动去除。然后再将集合转换回列表。
```python
def remove_duplicates(lst):
return list(set(lst))
```
2. 使用列表推导式:通过遍历原列表,只保留第一次出现的元素,将其放入新列表中。
```python
def remove_duplicates(lst):
return [x for i, x in enumerate(lst) if x not in lst[:i]]
```
3. 使用字典键的唯一性:遍历原列表,将元素作为字典的键,值设为任意非空值。由于字典的键具有唯一性,会自动去除重复元素。最后再将字典的键提取出来返回。
```python
def remove_duplicates(lst):
return list(dict.fromkeys(lst))
```
这些方法可以根据具体需求选择使用。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)