Why dtype = object?
Consider the second line, now with one variable assignment:
w = np.array(list(zip(*p.items()))) print(w)
Here w becomes an array consisting of a string of tuples and a string int. Whenever such mixed types are involved, the generic data type object ( dtype ) is used. In your code, you have k, z = w , and so even if ints are stored in z , they retain their dtype ( object ).
Cleaner way
In one line we can do
x, y, z = np.array([(x, y, z) for (x, y), z in p.items()]).T
Here a list is created that stores the tuples of the form (x, y, z) . Then it is converted to a 2D NumPy array. Finally, this array (or matrix) is transposed ( .T ), and then x , y and z assigned. Transposition is necessary, because otherwise the rows of the matrix will be assigned x , y and z (try to leave .T and see the effect for yourself).
source share