How to convert a python set to a numpy array?

Try this.

numpy.array(list(c))

Converting to list before initializing numpy array would set the individual elements to integer rather than the first element as the object.


Try:

numpy.fromiter(c, int, len(c))

This is twice as fast as the solution with list as a middle product.


Don't convert the numpy array to a set to perform exclusive-or. Use setxor1d directly.

>>> import numpy
>>> a = numpy.array([1,2,3,4,5,6])
>>> b = numpy.array([2,3,5])
>>> numpy.setxor1d(a, b)
array([1, 4, 6])

Do:

>>> numpy.array(list(c))
array([1, 4, 6])

And dtype is int (int64 on my side.)