next up previous
Next: Array shape Up: A sampling of useful Previous: Copying arrays

Counting vector elements with len

Assume we have the files generated from the subsection above. Then suppose we want to read a vector from a file and need to know how many elements we read. Here is an example:

>>> w = np.loadtxt("v.txt")
>>> print(w)
[ 1. -1.  1.]
n = len(w)
>>> print(n)

The function len gets the number of rows in the file. (The same len function works with standard Python objects, such as tuples and lists.)

Carleton DeTar 2018-02-12