查列表中出现重复的数据及出现次数

因公司业务需要,需要找出文件夹中相同文件名(不考虑文件格式的情况下)

# -*- coding:utf-8 -*-
from collections import Counter

fileNameList = [_.split('.')[0] for _ in os.listdir(html_path)]
b = dict(Counter(fileNameList))
pic_htm = [key for key, value in b.items() if value > 1]
htm = [key for key, value in b.items() if value < 2]
print(pic_htm)
print(htm)

结果如下:

print(b)
>>>{'011001900611_86708255': 2, '01100200011112597759': 1, '20200409193519_582hkn4qlu': 2, '20200508090807109': 2}
print(pic_htm)
>>>['011001900611_86708255', '20200409193519_582hkn4qlu', '20200508090807109']
print(htm)
>>>['01100200011112597759']

猜你喜欢

转载自blog.csdn.net/weixin_43124425/article/details/108345212