Python 在 for 循环中减少一个大列表以提高速度

标签 python list for-loop recursion

我试图获取一个包含 400 万个条目的列表,而不是遍历所有条目,而是减少 for 循环中枚举它们的列表。

减少标准在循环中找到。后面的一些 my_huge_list 元素包含 2 个连续元素的组合,可以立即丢弃它们。

这里我将从 my_huge_list 中删除包含 1,2 和 A,B 的子列表。

请注意,在我进入 for 循环之前,我事先并不知道 1,2 和 A,B 是非法的。

output_list = []

my_huge_list = [[0,1,2,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,3,4],[0,1,2,4],[0,1,2,3,4],[A,B],[0,1,3,A,B],[0,1,2,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,3,4],[0,1,2,4]...] #to 4m assorted entries

for sublist in my_huge_list[:]: 
   pair = None
   for item_index in sublist[:-1]: #Edit for Barmar.  each item in sublist is actually an object with attributes about allowed neighbors.
     if sublist[item_index +1] in sublist[item_index].attributes['excludes_neighbors_list']:
        pair = [sublist[item_index],sublist[item_index +1]]  #TODO build a list of pairs

   if pair != None: #Don't want pair in any item of output_list
      my_huge_list = [x for x in my_huge_list if not ','.join(pair) in str(x)]  #This list comprehension sole function to reduce my_huge_list from 4m item list to 1.7m items

  #if '1, 2' in str(sublist): #Don't want 1,2 in any item of output_list
        #my_huge_list = [x for x in my_huge_list if not '1, 2' in str(x)]  #This list comprehension sole function to reduce my_huge_list

  #elif 'A, B' in str(sublist): #Don't want A,B in any item of output_list
        #my_huge_list = [x for x in my_huge_list if not 'A, B' in str(x)]  #This list comprehension sole function to reduce my_huge_list from 1.7m item list to 1.1m items


  else:
     output_list.append(sublist) 


my_huge_list
>>>[[0,1,3,4],[0,1,3,4],[0,1,3,4],[0,1,3,4]...] 

不幸的是,“for 循环”似乎并没有变得更快,因为 my_huge_list 仍在所有 4m 条目上进行迭代,尽管列表理解很快就减少了它。

[my_huge_list不需要以任何顺序处理,也不需要在此循环后保留。]

[我考虑过将 for 循环变成一个子函数并使用 map 和浅拷贝,但无法弄清楚这种架构。]

[通过测试,我确信通过列表理解删除列表元素比暴力破解所有 4m 子列表更快。]

谢谢!

最佳答案

这是我的挖掘:

my_huge_list = [[0,1,2,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,3,4],[0,1,2,4],[0,1,2,3,4],['A','B'],[0,1,3,'A','B'],[0,'A','B'],[0,1,2,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,3,4],[0,1,2,4]] #to 4m assorted entries

# ... do whatever and return unwanted list... #

# ... if needed, convert the returned items into lists before putting into unwanted ... #

unwanted = [[1,2], ['A','B']]

index = 0
while index < len(my_huge_list):
    sublist = my_huge_list[index]
    next = True
    for u in unwanted:
        if u in [sublist[j:j+len(u)] for j in range(len(sublist)-len(u)+1)] or u == sublist:
            my_huge_list.pop(index)
            next = False
    index += next

print(my_huge_list)

# [[0, 1, 3, 4], [0, 1, 3, 4], [0, 1, 3, 4], [0, 1, 3, 4]]

这并不优雅,但它可以完成工作。一个巨大的警告是,在迭代时修改 list 是不好的业力(专业人士可能会对我摇头),但是处理 4 mil 的大小你可以理解我正在努力保存通过就地修改一些内存。

这也是可扩展的,因此如果您有多个不同大小的不需要的,它仍然应该从您的庞大列表中捕获它。如果您的元素大小为 1,请尝试匹配 my_huge_list 中的预期元素类型。例如如果您的 my_huge_list 有 [1],那么您不需要的也应该是 [1]。如果该元素是字符串而不是列表,则您的不需要的中将需要该字符串。然而,int/float 会破坏当前代码,因为您无法迭代它,但您可以在迭代不需要的代码之前添加额外的处理。

关于Python 在 for 循环中减少一个大列表以提高速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591755/

相关文章:

python - 添加列表的元素

ios - 在 "for"循环中使用 NSURL URLWithString 时应用程序崩溃

python - 忽略一次测试的类级别补丁装饰器

python - 了解 sklearn 中 CountVectorizer 中的 `ngram_range` 参数

python - Scrapy 和封装

list - 如何从 CLIPS 的列表中找到最大元素?

python - 如何提高 Python 中 odeint 的速度?

python - 使用多个键对 Python 列表进行排序

for 循环内的 C# 查询仅返回一个值

c++ - 数组在 while 循环中未正确初始化