[Python] convert itertools array into numpy array

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I'm creating this array:

    A=itertools.combinations(range(6),2)


    and I have to manipulate this array with numpy, like:

    A.reshape(..


    If the dimensions is A is high, the command list(A) is too slow.

    How can I "convert" an itertools array into a numpy array?


    Update 1: I've tried the solution of hpaulj, in this specific situation is a little bit slower, any idea?

    start=time.clock()

    A=it.combinations(range(495),3)
    A=np.array(list(A))
    print A

    stop=time.clock()
    print stop-start
    start=time.clock()

    A=np.fromiter(it.chain(*it.combinations(range(495),3)),dtype=int).reshape (-1,3)
    print A

    stop=time.clock()
    print stop-start


    Results:

    [[ 0 1 2]
    [ 0 1 3]
    [ 0 1 4]
    ...,
    [491 492 494]
    [491 493 494]
    [492 493 494]]
    10.323822
    [[ 0 1 2]
    [ 0 1 3]
    [ 0 1 4]
    ...,
    [491 492 494]
    [491 493 494]
    [492 493 494]]
    12.289898

    Continue reading...

Compartilhe esta Página