繁体   English   中英

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

[英]Iterate over a list and remove their elements multiple times

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

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

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

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

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

我认为这段代码对 go 很好;)

您显示的代码没有创建另一个列表,您也可以改进一下:

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

暂无
暂无

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

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