大循环嵌套的设计,反正以提高速度? [英] Large loop nest design, anyway to improve speed?
问题描述
我要遍历一个列表,并检查所有可能的组合在里面。目前,我使用了一系列的嵌套的for循环,显然我不是欣喜若狂,使用这种方法的速度。这是非常缓慢的,可能需要20至30分钟要经过所有组合。
I want to loop through a list and check all the possible combinations in it. Currently, I'm using a series of nested for loops, and obviously I'm not ecstatic with the speed using this method. It's very slow and can take 20-30 minutes to go through all the combinations.
for n1 in list1:
list.append(n1)
for n2 in list2:
list.append(n2)
for n3 in list2:
list.append(n3)
for n4 in list3:
list.append(n4)
for n5 in list3:
list.append(n5)
for n6 in list4:
list.append(n6)
for n7 in list4:
list.append(n7)
for n8 in list5:
list.append(n8)
for n9 in list5:
list.append(n9)
some logic that determines a value
list.remove(n9)
list.remove(n8)
list.remove(n7)
list.remove(n6)
list.remove(n5)
list.remove(n4)
list.remove(n3)
list.remove(n2)
list.remove(n1)
我不抱任何幻想,这是这样做的一个很好的方式。我只是想不出处理这更好的办法。还有最终被连击的一吨,但我需要计算每个人的价值。有5个名单,我需要检查从列表1包含一个的组合,并且两个点的列表2-5。
I have no illusions that this is a good way of doing this. I just can't think of a better way of handling this. There ends up being a TON of combos, but I need to calculate the value of each one. There are 5 lists, the combinations I need to check consist of one from list 1, and two spots from list 2-5.
如果任何人有关于如何改进这个或反正在Python中imrpove这个速度的建议,这将是AP preciated。
If anyone has suggestions on how to improve this or anyway in python to imrpove the speed of this, that would be appreciated.
将最终的组合看起来是这样的:
The final combination looks something like this:
List1[n1], list2[n2], list2[n3], list3[n4], list3[n5], list4[n6], list4[n7], list5[n8], list5[n9].
另外,还有一些组合,其中list2中,例如可 list2中[0],list2中[1]
和 list2中[1],list2中[ 0]
这对于我而言是一样的。消除这样的重复可以减少我的组合,但我不确定如何处理的。
Also, there are combinations where list2 for example could be list2[0],list2[1]
and list2[1], list2[0]
which for my purposes are the same thing. Eliminating duplicates like that could reduce my combinations, but I'm unsure of how to approach that.
推荐答案
看起来你想覆盖从的List1
一项摘下来,然后每两个来自所有组合 list2中
- list5
。如果正确的话,你当然可以使事情更加有效:
It looks like you want to cover all combinations of one item picked from list1
, then two each from list2
- list5
. If correct, you can certainly make things more efficient:
from itertools import chain, combinations, product
for comb in product(combinations(list1, 1),
combinations(list2, 2),
combinations(list3, 2), ...):
每个梳
将在形式((L1,),(12,12),(13,13),(14, 14),(15,15))
,但你可以使用这个变平了 chain.from_iterable
:
Each comb
will be in the form ((l1,), (l2, l2), (l3, l3), (l4, l4), (l5, l5))
, but you can flatten this out using chain.from_iterable
:
comb = list(chain.from_iterable(comb))
要获得 [L1,L2,L2,L3,L3,L4,L4,L5,L5]
。
对于一个整洁的改善,如果没有实际的效率,你可以定义使用列表和多少个项目,以从每个前面:
For a neatness improvement if not actual efficiency, you can define the lists to use and how many items to pick from each up front:
lists = [(list1, 1), (list2, 2), (list3, 2), (list4, 2), (list5, 2)]
for comb in product(*(combinations(l, n) for l, n in lists)):
这篇关于大循环嵌套的设计,反正以提高速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!