Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 51 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 51 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 51 -

Bild der Seite - 51 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 51 -

2.3 NumericalPythonArrays 51 In [12]: y[0] = 10.0 In [13]: y Out[13]: array([ 10., 1., 2.]) # ...as expected In [14]: x Out[14]: array([ 10., 1., 2.]) # ...x has changed too! Intuitively, it mayseem verystrange that changingan element inycausesa similar changeinx!Thethingis,however,thatourassignmenty = xdoesnotmakeacopy of thexarray.Rather,Pythoncreatesanother reference,namedy, to thesamearray object thatx refers to. That is, there is one array object with two names (x andy). Therefore,changingeitherxory, simultaneouslychanges“theother”(notethat this behaviordiffers from what we found in Sect.2.2.3 for single integer,float or string objects). To really get a copy that is decoupled from the original array, you may use the copy functionfromnumpy, In [15]: from numpy import copy In [16]: x = linspace(0, 2, 3) # x becomes array([ 0., 1., 2.]) In [17]: y = copy(x) In [18]: y Out[18]: array([ 0., 1., 2.]) In [19]: y[0] = 10.0 In [20]: y Out[20]: array([ 10., 1., 2.]) # ...changed In [21]: x Out[21]: array([ 0., 1., 2.]) # ...unchanged 2.3.5 SlicinganArray By use of a colon, you may work with a slice of an array. For example, by writing x[i:j], we address all elements from index i (inclusive) to j (exclusive) in an arrayx.An interactivesession illustrates this, In [1]: from numpy import linspace In [2]: x = linspace(11, 16, 6) In [3]: x Out[3]: array([ 11., 12., 13., 14., 15., 16.]) In [4]: y = x[1:5] In [5]: y Out[5]: array([ 12., 13., 14., 15.])
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python