我正在尝试确定可以构成29个元素序列的87个不同字符串的所有组合.我在python中使用组合来执行此操作,如果序列只有4个元素长,但不能处理29个,则可以正常工作.这是我正在使用的代码:
I'm trying to determine all the combinations of 87 different strings that could make up a 29 element sequence. I was using combinations in python to do this and it works fine if the sequence is only 4 elements long but it can't handle 29. This is the code I'm using:
combos = itertoolsbinations(testv, 29) usable_combos = [] for i in combos: usable_combos.append(i),但是代码在循环阶段失败.我认为这是某种内存问题,但不确定如何解决.有什么建议吗?
but the code fails at the loop stage. I assume this is some kind of memory issue but I'm not sure how to fix it. Any suggestions?
推荐答案您正在尝试将大量元组填充到此处的列表中. 101.416.867.967.028.166.758.360不同的元组,确切地说.这么大的数字我什至都不知道如何拼写,但是您可以以 101开头,几乎可以半六分之一.
You are trying to stuff a huge number of tuples into a list here. 101.416.867.967.028.166.758.360 different tuples, to be precise. A number so big I don't even know how to spell it out, but you can start with 101 and almost a half sextillion as an approximation.
当您将4个元素组合在一起时,只有 2.225.895个不同的组合(略高于200万个),这是很容易管理的,但是您将其推到了大多数计算机根本无法一次存储在内存中的水平.
When you made combinations of 4 elements, there were just 2.225.895 different combinations (a little over 2 million), something that is quite manageable, but you pushed it to levels that most computers simply cannot store in memory all at once.
与其将所有内容添加到列表中,然后再使用该列表,不如在循环时处理这些组合 :
Rather than add everything to a list, then use that list, you'd be better off processing those combinations as you loop:
for i in combos: # process i, move on或找到另一种解决问题的方法,该方法不涉及遍历所有可能的组合.也许有一些方法可以减少您实际需要考虑的组合数量?
or find a different approach to solving your problem, one that doesn't involve looping over all those possible combinations. Perhaps there are ways to reduce the number of combinations you actually need to consider?
顺便说一句,您可以只使用:
As an aside, rather than use a for loop and list.append(), you could have just used:
combos = itertoolsbinations(testv, 4) usable_combos = list(combos)创建列表.
更多推荐
在python中使用组合处理非常大的序列
发布评论