简体   繁体   English

在Python中创建和排序2D列表

[英]Create and sort a 2D list in Python

I am new to Python. 我是Python的新手。 I need to traverse the list of files in a directory, and have a 2D list of files (keys) with a value. 我需要遍历目录中的文件列表,并具有带值的文件(键)的2D列表。 Then I need to sort it based on their values, and delete the files with lower half of values. 然后,我需要根据它们的值对其进行排序,并删除值下半部分的文件。 How can I do that? 我怎样才能做到这一点?

This is what I did so far. 这是我到目前为止所做的。 I can't figure it out how to create such 2D array. 我不知道如何创建这样的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

Basically, the 2D array should look like [[file1, 0.876], [file2, 0.5], [file3, 1.24]] , which needed to be sorted out based on second indexes. 基本上,二维数组应看起来像[[file1, 0.876], [file2, 0.5], [file3, 1.24]] ,需要根据第二个索引进行排序。

Based on the comments, looks like I have to do this when appending: 根据评论,看起来我在添加时必须这样做:

mylist.append([file, value])

And for sorting, I have to do this: 对于排序,我必须这样做:

mylist.sort(key=lambda mylist: mylist[1])

I don't understand what this message means. 我不明白此消息的含义。

delete the files with lower half of values 删除值下半部分的文件

Does this mean that you have to select the files having value less than the midpoint between minimum and maximum values on the files or that you just have to select the lower half of the files? 这是否意味着您必须选择值小于文件最小值和最大值之间的中点的文件,或者只是选择文件的下半部分?

There isn't any need to use a 2D-array if the second coordinate depends on the first thanks to my_function . 如果my_function使第二个坐标依赖于第一个坐标,则无需使用2D数组。 Here is a function that does what you need: 这是一个满足您需要的功能:

from os import listdir as ls
from os import remove as rm
from os.path import realpath

def delete_low_score_files(dir, func, criterion="midpoint")
    """Delete files having low score according to function f

       Args:
           dir (str): path of the dir;
           func (fun): function that score the files;
           criterion (str): can be "midpoint" or "half-list";
       Returns:
           (list) deleted files.
    """

    files = ls(dir)
    sorted_files = sorted(files, key=func)    

    if criterion == "midpoint":
        midpoint = func(sorted_files[-1]) - func(sorted_files[0])
        files_to_delete = [f for f in sorted_files if func(f) < midpoint]

    if criterion == "half-list":
        n = len(sorted_files)/2
        files_to_delete = sorted_files[:n]

    for f in files_to_delete:
        rm(realpath(f))

    return files_to_delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM