我有两个长度不同的列表,它们是我用来过滤嵌套列表“ lst”的下限和上限的范围。为了节省空间,我只复制了部分数据10次。我想对上限和下限进行两次迭代,这是我的尝试:
import numpy as np
import itertools
lst = [[4.256, 3.8518], [2.2121, 1.6064], [3.9662, 3.2433], [5.1571, 5.8898], [4.4909, 3.7328], [9.38, 10.2276], [4.8912, 5.846], [4.5729, 3.5768], [6.25, 5.2267], [3.1019, 4.1603], [7.7822, 14.9629], [4.7673, 12.1189]]
lst_long = lst * 10
lower_limit = np.arange(1, 3, 0.1).tolist()
upper_limit = np.arange(9, 12, 0.1).tolist()
def create_combo(a, b):
for sublist1 in itertools.product(a,b):
for sublist2 in itertools.product(a,b):
yield sublist1[0], sublist1[1], sublist2[0], sublist2[1]
for lower1, upper1, lower2, upper2 in create_combo(lower_limit, upper_limit):
filtered_list = [sublist for sublist in lst_long if lower1<=sublist[0]<=upper1 and lower2<=sublist[1]<=upper2]
x = [lst[0] for lst in filtered_list]
y = [lst[1] for lst in filtered_list]
此代码现在需要9秒钟以上才能在我的PC上运行。随着范围的扩大,我确信运行时间的增加将成倍增加。因此,我正在寻找有关如何更有效地进行迭代的建议。任何软件包中是否有任何特殊功能可以加快该过程?
谢谢。