# [Numpy-discussion] itertools.combinations to numpy

Mario Moura moura.mario at gmail.com
Fri Dec 3 07:31:20 EST 2010

```Hi Folks

I have this situation

>>> from timeit import Timer
>>> reps = 5
>>>
>>> t = Timer('itertools.combinations(range(1,10),3)', 'import itertools')
>>> print sum(t.repeat(repeat=reps, number=1)) / reps
1.59740447998e-05
>>> t = Timer('itertools.combinations(range(1,100),3)', 'import itertools')
>>> print sum(t.repeat(repeat=reps, number=1)) / reps
1.74999237061e-05
>>>
>>> t = Timer('list(itertools.combinations(range(1,10),3))', 'import itertools')
>>> print sum(t.repeat(repeat=reps, number=1)) / reps
5.31673431396e-05
>>> t = Timer('list(itertools.combinations(range(1,100),3))', 'import itertools')
>>> print sum(t.repeat(repeat=reps, number=1)) / reps
0.0556231498718
>>>

You can see list(itertools.combinations(range(1,100),3)) is terrible!!

If you change to range(1,100000) your computer will lock.

So I would like to know a good way to convert <itertools.combinations
object> to ndarray? fast! without use list
Is it possible?

>>> x = itertools.combinations(range(1,10),3)
>>> x
<itertools.combinations object at 0x25f1520>
>>>

I tried this from
http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations

>>> numpy.fromiter(itertools.combinations(range(1,10),3), int, count=-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
>>>

and this from
http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations

import numpy
from itertools import *
from numpy import *

def combinations(iterable, r):
pool = tuple(iterable)
n = len(pool)
for indices in permutations(range(n), r):
if sorted(indices) == list(indices):
yield tuple(pool[i] for i in indices)

numpy.fromiter(combinations(range(1,10),3), int, count=-1)

>>> numpy.fromiter(combinations(range(1,10),3), int, count=-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
>>>

I like itertools.combinations performance but I need convert it to numpy.

Best Regards

mario

```