Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpacking an array in python

I have a variable data that is of (1000L, 3L) shape and I do the following to get the coordinates:

x = data[:,0]
y = data[:,1]
z = data[:,2]

Is there a way to unpack them? I tried but it doesn't work:

[x,y,z] = data1[:,0:3]
like image 460
SEU Avatar asked Sep 09 '17 17:09

SEU


People also ask

What is unpacking in Python?

Introduction. Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .

Can you unpack Numpy array?

To unpack elements of a uint8 array into a binary-valued output array, use the numpy. unpackbits() method in Python Numpy. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done.

Can we unpack a list in Python?

If you want to unpack the first few elements of a list and don't care about the other elements, you can: First, unpack the needed elements to variables. Second, pack the leftover elements into a new list and assign it to another variable.

How to unpack items from a list in Python?

In Python we can also destructure ( unpack) items from a list (Python doesn't have a native array data structure but a list looks identical to a Javascript array). The syntax would look like so: We can skip items in the list, just like in Javascript.

What is unpacking in Python?

What’s unpacking? Unpacking is the process of getting out stuff — iterables such as lists, tuples, and dictionaries. Think of it as opening a box and getting out different items like cables, headphones, or a USB. Unpacking in Python is similar to unpack a box in real life. Let’s translate this same example into code for a better understanding:

How to unpack a unique element of an iterable in Python?

We can unpack a unique element of an iterable. For example, you’d come up with something like this: If we want to unpack all the values of an iterable to a single variable, we must set up a tuple, hence adding a simple comma will be enough:

How to unpack tuples in Python?

In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable. Consider the following code for better understanding. Writing code in comment?


2 Answers

You could unpack using zip:

x, y, z = zip(*data[:, :3])
like image 45
Alexander Avatar answered Sep 23 '22 21:09

Alexander


You could simply transpose it before unpacking:

x, y, z = data.T

Unpacking "unpacks" the first dimensions of an array and by transposing the your array the size-3 dimension will be the first dimension. That's why it didn't work with [x, y, z] = data1[:, 0:3] because that tried to unpack 1000 values into 3 variables.

like image 153
MSeifert Avatar answered Sep 22 '22 21:09

MSeifert