AttributeError:Python“过滤器”对象没有属性“ sort”

时间:2019-09-03 11:16:00

标签: python-3.x sorting object filter

我遇到问题

AttributeError:“过滤器”对象没有属性“排序”

下面是整个错误消息:

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()

2 个答案:

答案 0 :(得分:2)

在python 3中,filter返回可迭代。并且您在iterable上调用sort方法,从而得到一个错误。 要么将迭代包装在列表中

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

或代替fileList.sort()以有序方法传递迭代器

fileList= sorted(fileList)

Python 3 doc for filter

答案 1 :(得分:0)

您正在使用Python3。Filter返回一个可迭代的filter对象,但没有sort方法。将过滤器对象包装在list中。

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