遍历列表并多次删除其元素

问题描述 投票:1回答:2

我在Python中有以下脚本,每30秒检查一次列表中的所有任务,当任务完成(失败,取消或完成)时,该任务将从原始任务中删除,然后再次跟踪其他任务任务。这样,代码运行良好。

问题是我不确定while循环的每次迭代是否都在创建一个新列表,因为整个过程可能要花费几个小时,所以我不想在内存中创建不必要的数据。

def download():
    while tasks:

        for task in tasks[:]:
            success = 0
            file_name = task[1]
            task_index = tasks.index(task)
            task_state = ee.data.getTaskStatus(task[0])[0]['state']

            print(task, task_state)

            if  task_state == FAILED:
                tasks.remove(tasks[task_index])

            elif task_state in [CANCEL_REQUESTED, CANCELLED]:
                tasks.remove(tasks[task_index])

            elif task_state == COMPLETED:
                tasks.remove(tasks[task_index])
                success = 1

            if success == 1:

                do_something()

        if tasks:
            time.sleep(30)
        else:
            print('Done!')
download()
python google-earth-engine
2个回答
1
投票

您显示的代码不会创建另一个列表,您也可以改善一点:

to_remove_states = {CANCEL_REQUESTED, CANCELLED, FAILED, COMPLETED}

def my_filter(taks):
    state = ee.data.getTaskStatus(task[0])[0]['state']
    if state in to_remove_states:
        if state == COMPLETED:
            do_something()   # should not be dependent on your tasks form download function
        return False
    return True

def download():
    while tasks:
        time.sleep(30)
        tasks  = list(filter(my_filter, tasks))
    print('Done!')

download()

1
投票

您的代码中没有创建新列表。但是您可能要缩短一点:

def download():
while tasks:

    for task in tasks[:]:
        success = 0
        file_name = task[1]
        task_index = tasks.index(task)
        task_state = ee.data.getTaskStatus(task[0])[0]['state']

        print(task, task_state)

        if task_state in [CANCEL_REQUESTED, CANCELLED, FAILED, COMPLETED]:
            tasks.remove(tasks[task_index])
        if task_state == COMPLETED:
            do_something()

    if tasks:
        time.sleep(30)
    else:
        print('Done!')
download()

我认为这段代码不错;)

© www.soinside.com 2019 - 2024. All rights reserved.