简体   繁体   English

我需要加速 python 中的嵌套循环,它会产生数万亿个循环

[英]I need to speed up a nested loop in python that makes trillions of loops

filtradas is around 500000 elements and _13 around 2000000 the minimum time i have achieved is like 4 hours using cython, but I need to do it in less than an hour, how can i do it? filtradas大约有 500000 个元素, _13大约有 2000000 个元素,我使用 cython 达到的最短时间是 4 小时,但我需要在不到一个小时的时间内完成,我该怎么做?

Both lists have string elements with 1, 2 or X the X can be changed for a 3两个列表都有带 1、2 或 X 的字符串元素,X 可以更改为 3

for i in filtradas:
        for x in _13:
            aciertos=0
            if i[0]==x[0]:
                aciertos+=1
            if i[1]==x[1]:
                aciertos+=1         
            if i[2]==x[2]:
                aciertos+=1
            if i[3]==x[3]:
                aciertos+=1
            if i[4]==x[4]:
                aciertos+=1
            if i[5]==x[5]:
                aciertos+=1         
            if i[6]==x[6]:
                aciertos+=1
            if i[7]==x[7]:
                aciertos+=1
            if i[8]==x[8]:
                aciertos+=1
            if i[9]==x[9]:
                aciertos+=1         
            if i[10]==x[10]:
                aciertos+=1
            if i[11]==x[11]:
                aciertos+=1
            if i[12]==x[12]:
                aciertos+=1
            if i[13]==x[13]:
                aciertos+=1
            if aciertos>=nroaciertos:
                filtradas13.append(i)
                break
    return filtradas13

numpy is worth a try. numpy值得一试。 In this example I suspect that the time to convert to a numpy array isn't worth the compare but if your data was imported into numpy in the first place, it may go faster.在此示例中,我怀疑转换为 numpy 数组的时间不值得比较,但如果您的数据首先导入 numpy,它可能会更快 go。

import numpy as np

_13_array = [np.array(list(x))for x in _13]
for i in filtradas:
    filtradas_array = np.array(list(i))
    for x in _13_array:
        aciertos = (x==filtradas_array).sum()
        if aciertos>=nroaciertos:
            filtradas13.append(i)
            break

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

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