属性错误:Python 'filter'对象没有属性'sort'



我遇到了问题

属性错误:"过滤器"对象没有属性"排序">

以下是整个错误消息:

Using TensorFlow backend.
Traceback (most recent call last):
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 231, in <module>
n_imgs=15*10**4, batch_size=32)
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 166, in keras_fit_generator
data_to_array(img_rows, img_cols)
File "D:/Data/PROMISE2012/Vnet3d_data/promise12_Unet_segmentation-master/promise12_segmentation-master/codes/train.py", line 48, in data_to_array
fileList.sort()
AttributeError: 'filter' object has no attribute 'sort'
Process finished with exit code 1
def data_to_array(img_rows, img_cols):
clahe = cv2.createCLAHE(clipLimit=0.05, tileGridSize=(int(img_rows/8),int(img_cols/8)) )
fileList =  os.listdir('TrainingData/')
fileList = filter(lambda x: '.mhd' in x, fileList)
fileList.sort()

在python3中,过滤器返回可迭代。 并且您正在对可迭代调用排序方法,因此出现错误。 在列表中包装可迭代

fileList = list(filter(lambda x: '.mhd' in x, fileList))

或者代替fileList.sort()在排序方法中传递可迭代

fileList= sorted(fileList)

用于过滤器的 Python 3 文档

你正在使用Python 3。 筛选器返回一个可迭代的filter对象,但它没有sort方法。 将过滤器对象包装在list中。

fileList = list(filter(lambda x: '.mhd' in x, fileList))

最新更新