0

I have a code;

x = np.linspace(0, 12, 200, False, True, None)

when I print x, I get;

(array([ 0.  ,  0.06,  0.12,  0.18,  0.24,  0.3 ,  0.36,  0.42,  0.48,
        0.54,  0.6 ,  0.66,  0.72,  0.78,  0.84,  0.9 ,  0.96,  1.02,
        1.08,  1.14,  1.2 ,  1.26,  1.32,  1.38,  1.44,  1.5 ,  1.56,
        1.62,  1.68,  1.74,  1.8 ,  1.86,  1.92,  1.98,  2.04,  2.1 ,
        2.16,  2.22,  2.28,  2.34,  2.4 ,  2.46,  2.52,  2.58,  2.64,
        2.7 ,  2.76,  2.82,  2.88,  2.94,  3.  ,  3.06,  3.12,  3.18,
        3.24,  3.3 ,  3.36,  3.42,  3.48,  3.54,  3.6 ,  3.66,  3.72,
        3.78,  3.84,  3.9 ,  3.96,  4.02,  4.08,  4.14,  4.2 ,  4.26,
        4.32,  4.38,  4.44,  4.5 ,  4.56,  4.62,  4.68,  4.74,  4.8 ,
        4.86,  4.92,  4.98,  5.04,  5.1 ,  5.16,  5.22,  5.28,  5.34,
        5.4 ,  5.46,  5.52,  5.58,  5.64,  5.7 ,  5.76,  5.82,  5.88,
        5.94,  6.  ,  6.06,  6.12,  6.18,  6.24,  6.3 ,  6.36,  6.42,
        6.48,  6.54,  6.6 ,  6.66,  6.72,  6.78,  6.84,  6.9 ,  6.96,
        7.02,  7.08,  7.14,  7.2 ,  7.26,  7.32,  7.38,  7.44,  7.5 ,
        7.56,  7.62,  7.68,  7.74,  7.8 ,  7.86,  7.92,  7.98,  8.04,
        8.1 ,  8.16,  8.22,  8.28,  8.34,  8.4 ,  8.46,  8.52,  8.58,
        8.64,  8.7 ,  8.76,  8.82,  8.88,  8.94,  9.  ,  9.06,  9.12,
        9.18,  9.24,  9.3 ,  9.36,  9.42,  9.48,  9.54,  9.6 ,  9.66,
        9.72,  9.78,  9.84,  9.9 ,  9.96, 10.02, 10.08, 10.14, 10.2 ,
       10.26, 10.32, 10.38, 10.44, 10.5 , 10.56, 10.62, 10.68, 10.74,
       10.8 , 10.86, 10.92, 10.98, 11.04, 11.1 , 11.16, 11.22, 11.28,
       11.34, 11.4 , 11.46, 11.52, 11.58, 11.64, 11.7 , 11.76, 11.82,
       11.88, 11.94]), 0.06)

I want to find how many variables are returned. To do this I've tried

print (len(x))

which I get 2.

This can't be correct, can it? Are there only two variables as one are the False in [] then the True. Or have I made a mistake somewhere If so what do I need to do to get the number of variables returned.

JuhBuh
  • 9
  • 1

3 Answers3

1

You have set the retstep parameter to True, thus you get back a tuple consisting of your array, and the step size. You can access your array by x[0] and its length by len(x[0]). But what I would rather do is the following:

x, stepsize = np.linspace(0, 12, 200, False, True, None)

And then you really have your array in x, so len(x) would work as expected.

zsomko
  • 581
  • 2
  • 6
0

In numpy, shape allows you to get the dimension of an array. For example here x.shape, or x[0].shape. The problem is that you are also returning the step at which your values are generated, so that you have your array of values and the step, hence why len(x) gives you 2, because x contains two objects. Note also that you are excluding the last value in your call to linspace(), which means that you do not reach 12 in the array you generate.

Patol75
  • 4,342
  • 1
  • 17
  • 28
0

The output that you get is composed a tuple with two elements. The first element of x is a numpy array of length 200, and the second element of x is a number (0.06). This is how you can find this:

import numpy as np

x = np.linspace(0, 12, 200, False, True, None)

print x

>>> (np.array([ 0.  ,  0.06,  0.12,  0.18,  0.24,  0.3 ,  0.36,  0.42,  0.48,
    0.54,  0.6 ,  0.66,  0.72,  0.78,  0.84,  0.9 ,  0.96,  1.02,
    1.08,  1.14,  1.2 ,  1.26,  1.32,  1.38,  1.44,  1.5 ,  1.56,
    1.62,  1.68,  1.74,  1.8 ,  1.86,  1.92,  1.98,  2.04,  2.1 ,
    2.16,  2.22,  2.28,  2.34,  2.4 ,  2.46,  2.52,  2.58,  2.64,
    2.7 ,  2.76,  2.82,  2.88,  2.94,  3.  ,  3.06,  3.12,  3.18,
    3.24,  3.3 ,  3.36,  3.42,  3.48,  3.54,  3.6 ,  3.66,  3.72,
    3.78,  3.84,  3.9 ,  3.96,  4.02,  4.08,  4.14,  4.2 ,  4.26,
    4.32,  4.38,  4.44,  4.5 ,  4.56,  4.62,  4.68,  4.74,  4.8 ,
    4.86,  4.92,  4.98,  5.04,  5.1 ,  5.16,  5.22,  5.28,  5.34,
    5.4 ,  5.46,  5.52,  5.58,  5.64,  5.7 ,  5.76,  5.82,  5.88,
    5.94,  6.  ,  6.06,  6.12,  6.18,  6.24,  6.3 ,  6.36,  6.42,
    6.48,  6.54,  6.6 ,  6.66,  6.72,  6.78,  6.84,  6.9 ,  6.96,
    7.02,  7.08,  7.14,  7.2 ,  7.26,  7.32,  7.38,  7.44,  7.5 ,
    7.56,  7.62,  7.68,  7.74,  7.8 ,  7.86,  7.92,  7.98,  8.04,
    8.1 ,  8.16,  8.22,  8.28,  8.34,  8.4 ,  8.46,  8.52,  8.58,
    8.64,  8.7 ,  8.76,  8.82,  8.88,  8.94,  9.  ,  9.06,  9.12,
    9.18,  9.24,  9.3 ,  9.36,  9.42,  9.48,  9.54,  9.6 ,  9.66,
    9.72,  9.78,  9.84,  9.9 ,  9.96, 10.02, 10.08, 10.14, 10.2 ,
   10.26, 10.32, 10.38, 10.44, 10.5 , 10.56, 10.62, 10.68, 10.74,
   10.8 , 10.86, 10.92, 10.98, 11.04, 11.1 , 11.16, 11.22, 11.28,
   11.34, 11.4 , 11.46, 11.52, 11.58, 11.64, 11.7 , 11.76, 11.82,
   11.88, 11.94]), 0.06)

print type(x) 
>>> <type 'tuple'>

print len(x[0])  #This is the length of the first element of x (x[0]).
>>> 200 

The reason why print len(x) returns 2 is because, x is a tuple with 2 elements. At the same time x[0] (i.e., the first element of x) is a numpy array with 200 elements. The second element of x (x[1]) is the number 0.06.

Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52