Computer Science

Python Arrays

Python arrays are data structures used to store a collection of elements of the same type. They are similar to lists but are more efficient for numerical operations. Arrays in Python are provided by the array module and can be created using the array() function, allowing for efficient manipulation and storage of data in a contiguous block of memory.

Written by Perlego with AI-assistance

3 Key excerpts on "Python Arrays"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Data Analysis for Corporate Finance
    eBook - ePub

    Data Analysis for Corporate Finance

    Building financial models using SQL, Python, and MS PowerBI

    Our next step is to learn to use the NumPy library. There is an important aspect you should be mindful of anytime you explore a new Python module. Python provides the language and the syntax to write code; however, each module has been designed, defined, and structured to achieve specific goals and objectives; hence, you might find yourself in the situation where the command that retrieves a certain outcome in pure Python might retrieve something completely different in the context of a module.
    From Pure Python Lists to NumPy Arrays
    To see the difference in notation and meanings, let us kick off using pure Python. First, let us create a vector using a list object. The Python Primer appendix reviews and explains the different types of Python data structures, I encourage you to read it if you need to get familiar with them.
    A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value inside of a list is called an item or element. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets. A mutable object allows the object to change its elements after it has been created. Data structures like tuples, which are immutable, do not allow for post creation changes.
    I will pause for a second to highlight the importance of model blueprint before you start coding. Financial models are living entities. They start small and simple, but as time goes by, the complexity increases exponentially. A small detail such as using a list or a tuple might look trivial, but it could become quite important to have used a tuple to secure data immutability for something like input variables, rather than using a list object where elements are subject to data manipulations. Take the time to use a piece of paper to sketch how your model should be and keep refining these details as your model grows.
    Now, let’s go back to NumPy. We begin by creating a simple Python list. A list of numbers shares some common features with a NumPy array. A NumPy array is a grid of the same type of numerical values, indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
  • Python for ArcGIS Pro
    • Silas Toms, Bill Parker, Dr. Christopher Tucker, Rene Rubalcava(Authors)
    • 2022(Publication Date)
    • Packt Publishing
      (Publisher)
    The core code of NumPy is written in the C programming language. This allows for better memory management of large arrays of data and quicker processing of data. Python, as an interpreted language, must be converted into byte-code to be executed, which can make the code run more slowly for some applications. Owing to that C code core, NumPy is more efficient and faster.

    NumPy arrays versus Python lists

    NumPy arrays are often compared to the built-in Python list data type. Both lists and ndarrays are ordered data structures that are mutable and enclosed in square brackets. However, there are some major differences between them. 
    A Python list can contain a combination of different types of data, while NumPy arrays can only contain one type of data per array. This means that there is no type checking on each object in the list. NumPy arrays are also faster when accessing data, and are more compact in memory use.
    Data objects in an array are stored with much less metadata describing the object (size, reference count, object type, and object value are stored for Python lists). Python lists therefore balloon the memory usage, making it much slower to access or iterate each object.
    Read more about NumPy here: https://numpy.org/ .
    Read the basic introduction to NumPy here: https://numpy.org/doc/stable/user/absolute_beginners.html .

    Importing NumPy

    The first step toward using NumPy is to import the module. Open a new Notebook in ArcGIS Pro and type the following into the first cell:
    import numpy as np
    Run the cell. Using the variable np to represent the numpy module is not required, but it is an accepted shorthand for Python code that makes it easier to access the submodules and properties of NumPy.

    Basics of NumPy for rasters

    Using NumPy for rasters is very straightforward. Rasters are data organized into regular rows and columns, and may have multiple bands of data. These data behaviors can be precisely recreated using NumPy arrays, which can have any number of rows or columns, as well as multiple dimensions.

    Creating an array 

    Often in GIS you must create rasters for analyses. These arrays may need to be blank, allowing you to accumulate values from inputs to a continuous surface based on location; all one value to create a constant raster; or merged with vector data inputs such as GeoJSON files or shapefiles. All of these are possible with NumPy arrays.
  • Bioinformatics Algorithms
    eBook - ePub

    Bioinformatics Algorithms

    Design and Implementation in Python

    • Miguel Rocha, Pedro G. Ferreira(Authors)
    • 2018(Publication Date)
    • Academic Press
      (Publisher)
    Programs are developed for the solution of a single or multiple tasks. They consist of a set of instructions defining the information flow. During the execution of a program, functions can be called and the state of variables and objects are altered dynamically.
    Within functions and programs, a set of statements are used to describe the data flow, including testing or control structures for conditional and iterative looping. We will start by looking at some of Python's pre-defined variables and functions, and will proceed to look at the algorithmic constructs that allow for the definition of novel functions or programs.

    2.2 Variables and Pre-Defined Functions

    2.2.1 Variable Types

    Variables are entities defined by their names, referring to values of a certain type, which may change their content during the execution of a program. Types can be atomic or define complex data structures that can be implemented through objects, instances of specific classes. Types can be either pre-defined, i.e. already part of the language, or defined by the programmer.
    Pre-defined variable types in Python can be divided into two main groups: primitive types and containers. Primitive types include numerical data, such as integer (int ) or floating point (float ) (to represent real numbers). Boolean, a particular case of the integer type, is a logical type (allowing two values, True or False ).
    Python has several built-in types that can handle and manage multiple variables or objects at once. These are called containers and include the string, list, tuple, set, and dictionary types. These data types can be further sub-divided according to the way their elements are organized and accessed. Strings, lists, and tuples are sequence types since they have an implicit order of their elements, which can be accessed by an index value.
    Sets and dictionaries represent a collection of unordered elements. The set type implements the mathematical concept of sets of elements (any object), where the position or order of the elements is not maintained. Dictionaries are a mapping type, since they rely on a hashing strategy to map keys to the corresponding values, which can be any object.