Interactive Mode V / S Ide Python

1

One question; Why do I do this in interactive mode from python and from "spyder / ninja" does not work?

Python

»> from numpy import *
»> a = array([10,20,30,40])
»> append(a,50)
array([10, 20, 30, 40, 50])
»> 

Spyder and Ninja

from numpy import *
a = array([10,20,30,40])
append(a,50)
print a
[10 20 30 40]
    
asked by The_Chapu 21.06.2016 в 00:14
source

3 answers

2

In interactive mode returns the result of the operation. It does so because it is practical (and interactive). If you want to keep the result you should save it in a new variable. Your example would look like this:

import numpy as np # from numpy import * es SIEMPRE una malísima idea
a = np.array([10,20,30,40])
b = np.append(a,50)
print(b)

And now, the result will be:

[10 20 30 40 50]

Another thing, the use of numpy.append is usually very expensive (in process and memory) and there are usually better methods to extend a numpy.array .

    
answered by 21.06.2016 в 08:40
1

The code is not the same in the first case as in the second case. You need to print the variable a in the interactive console.

The append function allows you to add a new element to a list. This function, creates a new list and returns it , and that's what happens in the first case, after calling the function, it returns a new list. However, the original list stored in a has not been altered. You can check it by printing it on the screen:

>>> print a
[10 20 30 40]

Greetings.

    
answered by 21.06.2016 в 01:04
1

The function print() internally calls the representation str() (or method __str__() ) of the object, while the interactive mode is using the function repr() (or the __repr__() method). The first one offers a "human-friendly" representation of the object, while the second one is more destined to the computer and in principle it should be evaluable by the interpreter to recover the original object.

If we run everything in interactive mode:

In [1]: import numpy as np

In [2]: a = np.array([10, 20, 30, 40])

In [3]: a
Out[3]: array([10, 20, 30, 40])

In [4]: print(a)
[10 20 30 40]

In [5]: str(a)
Out[5]: '[10 20 30 40]'

In [6]: a.__str__()
Out[6]: '[10 20 30 40]'

In [7]: repr(a)
Out[7]: 'array([10, 20, 30, 40])'

In [8]: a.__repr__()
Out[8]: 'array([10, 20, 30, 40])'

Here more information in English about the differences between str() and repr() .

    
answered by 21.06.2016 в 17:14