我正在尝试遍历文件夹并删除任何重复图像(但名称不同)的文件。运行此脚本后,除一个文件外,所有文件都将被删除。大约 5,000 个中至少有十几个是独特的。任何帮助理解为什么会发生这种情况的帮助将不胜感激。
import os
import cv2
directory = r'C:\Users\Grid\scratch'
for filename in os.listdir(directory):
a=directory+'\\'+filename
n=(cv2.imread(a))
q=0
for filename in os.listdir(directory):
b=directory+'\\'+filename
m=(cv2.imread(b))
comparison = n == m
equal_arrays = comparison.all()
if equal_arrays==True and q==1:
os.remove(b)
q=1
Run Code Online (Sandbox Code Playgroud) 示例代码:
class hello:
cls_var_name='Hello_Class'
def setdata(self, value):
self.data=value
fun1_var_name='Setdata_Function'
def showdata(self):
fun2_var_name='Showdata_Function'
print(self.data)
Run Code Online (Sandbox Code Playgroud)
因此,借助该dir()函数,我们可以列出一个类的所有属性。但如何列出类函数内的变量/属性。
演示:
>>> dir(hello)
['__doc__', '__module__', 'cls_var_name', 'setdata', 'showdata']
>>>
>>> dir(hello.setdata)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
>>>
>>> dir(hello.showdata)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
Run Code Online (Sandbox Code Playgroud)
在上面的输出中,您可以在结果中看到类的属性列表dir(hello):
'cls_var_name', …
假设我们有以下列表
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
Run Code Online (Sandbox Code Playgroud)
现在我想将每 3 个数字相加以提供 6 个列表的长度,
[6, 15, 24, 33, 42, 51]
Run Code Online (Sandbox Code Playgroud)
我想在 python 中执行此操作...请帮忙!(我的问题措辞很奇怪吗?)
直到现在我尝试过
z = np.zeros(6)
p = 0
cc = 0
for i in range(len(that_list)):
p += that_list[i]
cc += 1
if cc == 3:
t = int((i+1)/3)
z[t] = p
cc = 0
p = 0
Run Code Online (Sandbox Code Playgroud)
但它没有用......