我是Python的新手。我需要遍历目录中的文件列表,并有一个带有值的文件(键)的2D列表。然后我需要根据它们的值对其进行排序,并删除值较小的文件。我该怎么做呢?
这就是我到目前为止所做的。我不知道如何创建这样的二维数组。
dir = "images"
num_files=len(os.listdir(dir))
for file in os.listdir(dir):
print(file)
value = my_function(file)
#this is wrong:
_list[0][0].append(value)
#and then sorting, and removing the files associated with lower half
基本上,2D数组应该看起来像[[file1, 0.876], [file2, 0.5], [file3, 1.24]]
,需要根据第二个索引进行排序。
发布于 2018-09-28 21:01:36
根据评论,看起来我必须在添加的时候这样做:
mylist.append([file, value])
为了进行排序,我必须这样做:
mylist.sort(key=lambda mylist: mylist[1])
https://stackoverflow.com/questions/52562544
复制相似问题