迭代时从列表中删除项目而不使用Python中的额外内存 [英] Remove items from a list while iterating without using extra memory in Python

查看:90
本文介绍了迭代时从列表中删除项目而不使用Python中的额外内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:我有一长串的元素,我想迭代并根据条件检查每个元素。根据条件的结果,我想删除列表的当前元素,并像往常一样继续迭代它。

My problem is simple: I have a long list of elements that I want to iterate through and check every element against a condition. Depending on the outcome of the condition I would like to delete the current element of the list, and continue iterating over it as usual.

我已经阅读了一些其他线程这件事。提出两种解决方案。要么从列表中创建一个字典(这意味着要复制已经填满我所有RAM的所有数据)。要么反向走这个列表(这打破了我想要实现的算法的概念)。

I have read a few other threads on this matter. Two solutions seam to be proposed. Either make a dictionary out of the list (which implies making a copy of all the data that is already filling all the RAM in my case). Either walk the list in reverse (which breaks the concept of the alogrithm I want to implement).

有没有比这更好或更优雅的方法呢?

Is there any better or more elegant way than this to do it ?

def walk_list(list_of_g):
    g_index = 0
    while g_index < len(list_of_g):
        g_current = list_of_g[g_index]
        if subtle_condition(g_current):
            list_of_g.pop(g_index)
        else:
            g_index = g_index + 1


推荐答案

如果你绝对需要,这是一个替代答案从原始列表中删除项目,并且您没有足够的内存来制作副本 - 自己将项目向下移动到列表中:

Here is an alternative answer for if you absolutely have to remove the items from the original list, and you do not have enough memory to make a copy - move the items down the list yourself:

def walk_list(list_of_g):
    to_idx = 0
    for g_current in list_of_g:
        if not subtle_condition(g_current):
            list_of_g[to_idx] = g_current
            to_idx += 1
    del list_of_g[to_idx:]

这将移动每个项目(实际上是指向每个项目的指针恰好一次,因此将是O(N)。函数末尾的del语句将删除列表末尾的任何不需要的项目,我认为Python足够智能,可以调整列表大小,而无需为列表的新副本分配内存。

This will move each item (actually a pointer to each item) exactly once, so will be O(N). The del statement at the end of the function will remove any unwanted items at the end of the list, and I think Python is intelligent enough to resize the list without allocating memory for a new copy of the list.

这篇关于迭代时从列表中删除项目而不使用Python中的额外内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆