[Tutor] Question

Steven D'Aprano steve at pearwood.info
Sun Dec 10 05:41:04 EST 2017


Hello Khabbab Zakaria,

On Sun, Dec 10, 2017 at 11:18:16AM +0530, Khabbab Zakaria wrote:
> I am working on a program where I found the line:
> x,y,z = np.loadtext('abcabc.txt', unpack= True, skiprows =1)
> What does the x, y, z thing mean?

"x, y, z = ..." is iterable unpacking. The right hand side has to be an 
iterable (any object that can be iterated over in a for-loop) such as a 
list, a tuple, a set, or similar. For example:


    x, y, z = [1, 2, 3]


will set x = 1, y = 2, z = 3. It is an error if the object on the right 
hand side has too few or too many items:


    a, b, c, d, e = (1, 2, 3) # Too few items

    a, b, c, d, e = (1, 2, 3, 4, 5, 6, 7)  # Too many items


> What does the unpack= True mean?

I'm afraid you will need to read the numpy documentation for that. I 
tried looking at it myself, but the version of numpy I have seems to be 
too old:

py> import numpy as np
py> help(np.loadtext)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'loadtext'



-- 
Steve


More information about the Tutor mailing list