ordinary - 6 months ago 88

Python Question

I have a simple question. fy=[2,6,5].When I type print(fy) it gives me output:

[2, 6, 5]

Then, s = np.array(fy)

It gives me output:

[2 6 5]

What does it mean and what is the difference?

I need to find the frequency by FFT (np.fft.fftfreq).

I mean, I have time points as x- axis. And I have these values above as y- axis.

Answer

**The question changed, so here is a new answer:**

When writing `fy=[2,6,5]`

you create a python list.

```
>>> fy = [2, 6, 5]
>>> print(fy)
[2, 6, 5]
>>> print(type(fy))
<type 'list'>
```

but with `s = np.array(fy)`

you create a numpy array.

```
>>> import numpy as np
>>> s = np.array(fy)
>>> print(s)
[2 6 5]
>>> print(type(s))
<type 'numpy.ndarray'>
```

As you can see the printed representation of the two objects differ in that numpy arrays don't separate values with commas. The guys who wrote numpy decided to skip the commas, presumably to reduce the visual clutter when printing numpy arrays.

**Original answer:**

In python, only the first (i.e. `[2, 3.1, 4.6, 3.5]`

) is valid. The latter (i.e. `[2 3.1 4.6 3.5]`

) is a syntax error. This is different from e.g. Matlab where both your examples would be correct.

See this page for some examples of how to create numpy arrays, and this page for differences between Matlab and numpy.