在python中使用组合处理非常大的序列

编程入门 行业动态 更新时间:2024-10-25 22:30:20
本文介绍了在python中使用组合处理非常大的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试确定可以构成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中使用组合处理非常大的序列

本文发布于:2023-11-11 11:00:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   非常大   序列   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!