Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unzip a dictionary of coordinates and values

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?

like image 290
Baz Avatar asked Feb 12 '18 08:02

Baz


1 Answers

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.

like image 129
asongtoruin Avatar answered Sep 30 '22 23:09

asongtoruin