Background:
I have a list of 44906 elements: large = [1, 60, 17, ...]. I also have a personal computer with limited memory (8 GB) running under Ubuntu 14.04.4 LTS.
Purpose:
I need to find all paired combinations largein a memory-efficient manner, without filling out the list with all combinations in advance.
The problem and what I have tried so far:
When I use itertools.combinations(large, 2)and try to assign it to a list, my memory immediately becomes full, and I get very slow performance. The reason for this is that the number of pairwise combinations looks like n*(n-1)/2, where nis the number of elements in the list.
The number of combinations for n=44906is displayed on 44906*44905/2 = 1008251965. A list with many entries is too large to hold in memory. I would like to be able to create a function so that I can connect a number ito find ith pair combination of numbers in this list and a way to somehow dynamically calculate this combination without reference to the list of elements 1008251965, which cannot be stored in memory.
An example of what I'm trying to do:
Say I have an array small = [1,2,3,4,5]
In the configuration in which I have the code, it itertools.combinations(small, 2)will return a list of tuples as such:
[(1, 2),
(1, 3),
(1, 4),
(1, 5),
(2, 3),
(2, 4),
(2, 5),
(3, 4),
(3, 5),
(4, 5)]
A function call like this: `find_pair (10) 'will return:
(4, 5)
giving the 10th record in a potential array, but without first triggering the entire combinatorial explosion.
, , , :
>>> from itertools import combinations
>>> it = combinations([1, 2, 3, 4, 5], 2)
>>> next(it)
(1, 2)
>>> next(it)
(1, 3)
>>> next(it)
(1, 4)
>>> next(it)
(1, 5)
, , next() 10 , 10- , , 10- , .
- , , ? , , ?