1

I am trying to assign the output of itertools combinations function to a numpy array and it is creating a blank array. Why does this code not work correctly?

    import numpy as np
    from itertools import combinations

    A=[10,5,7,90,4,200,64]
    B=combinations(A,5)
    NA=np.zeros([5,21],dtype=np.uint8)

    print (list(B))

    NA=list(B)
    print (NA)        
2
  • 1
    B is a generator once exhausted, you can't iterrate again. list(B) will consume whole generator Commented May 26, 2020 at 20:19
  • B is a generator, which can be used only once. The 2nd list(B) operates on a used, empty generator. Also that first NA=... assignment does nothing for you; the 2nd overwrites it. NA = np.array(list(combinations(A,5))) is all you need. (along with some more basic Python study :) ). Commented May 26, 2020 at 20:20

1 Answer 1

1

Save the output of generator B when you exhaust it in order to use it later:

A=[10,5,7,90,4,200,64]
B=combinations(A,5)
#this is line is not really required unless you use it in between your lines here
NA=np.zeros([5,21],dtype=np.uint8)

B=list(B)
print(B)

NA=np.array(B)
print (NA)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.