I wish to populate the values x
,y
and z
, where x
are the x coordinates, y
the y coordinates and z
the associated values for each coordinate, as defined by p
. Here is how I am doing it:
p = {(1,2): 10, (0,2):12, (2,0):11}
k,z = np.array(list(zip(*p.items())))
x,y = np.array(list(zip(*k)))
Is there another more readable way? Perhaps there is something in numpy or scipy for doing this?
Why does z
result in array([10, 11, 12], dtype=object)
, while x
an y
do not include dtype=object
?
How about as a one-liner?
x, y, z = np.array([(x, y, z) for (x, y), z in p.items()]).T
This makes it clearer where the values are coming from, without the unnecessary and unused k
. Additionally, you should not have the dtype
issue with this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With