Array is a functional mutable sequence inheriting from Python's built-in list.

Related tags

Data Structuresfunct
Overview

funct.Array

Array is a functional mutable sequence inheriting from Python's built-in list. Array provides 100+ higher-order methods and more functionality to the built-in list, making operations on sequences simpler and one-liners neater with no third party packages required.

Array provides a combination of python built-ins, features found in NumPy arrays, and higher-order methods common to functional languages without the weird semantics of the builtins, still preserving the same functionality and the dynamic nature of the built-in list.

Documentation

funct.Array is available on PyPi and can be installed with pip

$ pip install funct

Array Creation

Arrays can be created either with multiple arguments or by providing a sequence as an argument.

>>> from funct import Array
>>> Array(1, 2, 3)
Array(1, 2, 3)
>>> Array([1, 2, 3])
Array(1, 2, 3)

An Array can also be initialized with the static zeros method or the pad method.

Python built-in sequences (including nested ones) lists, tuples and ranges are converted to Arrays on instantiation. However, other iterables e.g. generators and numpy ndarrays are converted to Arrays only if the argument consists of a single iterable. The elements can be converted to Arrays by calling the toArray method.

>>> Array(np.zeros(3))
Array(0.0, 0.0, 0.0)
>>> Array(np.zeros(3), np.zeros(3))
Array(array([0., 0., 0.]), array([0., 0., 0.])
>>> Array(np.zeros(3), np.zeros(3)).toArray()
Array(Array(0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0))

Arrays provide static methods arange, linspace and logspace for creating linearly or logarithmically spaced Arrays.

Examples

Chaining multiple functions with Arrays result in cleaner code without multiple nested functions, e.g.

a.zip(b).map(func1).filter(func2).forall(func3)

# vs. in traditional python

all(map(func3, filter(func2, map(func1, zip(a, b)))))

where a & b are Arrays and func1, func2 & func3 some functions.

Multiplying elements in a sequence with a constant
#  In traditional python the multiplication could be implemented using list comprehensions as follows
>>> nums = [1, 2, 3, 4, 5]
>>> [a * 10 for a in nums]
[10, 20, 30, 40, 50]

#  With Arrays multiplication simplifies to
>>> from funct import Array
>>> nums = Array(nums)
>>> nums.mul(10)
Array(10, 20, 30, 40, 50)
Multiplying two sequences element-wise
#  Traditional python
>>> nums2 = [11, 12, 13, 14, 15]
>>> [a * b for a, b in zip(nums, nums2)]
[11, 24, 39, 56, 75]

#  With Arrays
>>> nums.mul(nums2)
Array(11, 24, 39, 56, 75)

Same syntax applies for all mathematical operators; add, pow, mod, gt, lt, etc.

Selecting values greater than some number
#  Traditional python
>>> n = 2
>>> nums1 = [1, 2, 3, 4, 5]
>>> [x for x in nums if x > n]
[3, 4, 5]

#  With Arrays
>>> nums[nums > n]
Array(3, 4, 5)
Finding idex-wise maximum of sequences
>>> nums1 = Array(1, 2, 3, 4, 5)
>>> nums2 = Array(5, 4, 3, 2, 1)
>>> nums1.zip(nums2).map(max)
Array(5, 4, 3, 4, 5)
Splitting an Array based on type
>>> arr = Array(1, 2, "a", "b")
>>> arr.groupBy(type)[:, 1]  # group by type and select the 2nd element of the tuples
Array(Array(1, 2), Array('a', 'b'))
Multithreading/processing

Arrays also support parallel and concurrent execution. Functions applied to Arrays can be parallelized with the parmap and parstarmap methods. The same methods can be run asynchronously with the asyncmap and asyncstarmap methods.

>>> Array(1, 2, 3).parmap(some_heavy_func)
>>> Array(1, 2, 3).asyncmap(some_other_func)

Indexing

Array indexing is a combination of standard Python sequence indexing and numpy-style indexing. Array supports

  • Standard Python indexing (single element indexing, slicing)
  • Index arrays
  • Boolean masking
  • Multidimensional indexing

Examples

Standard Indexing
>>> a = Array(1, 2, 3)
>>> a[0]
1
>>> a[:2]
Array(1, 2)
Index Arrays
>>> a = Array('a', 'b', 'c', 'd')
>>> a[[1, 3]]
Array('b', 'd')
Boolean masking
>>> a = Array(1, 2, 3, 4)
>>> a[[True, False, False, True]]
Array(1, 4)
Multidimensional indexing
>>> a = Array((1, 2), (3, 4), (5, 6))
>>> a[:, 0]
Array(1, 3, 5)

Note that when indexing 'ragged' nested Arrays multidimensional indexing may raise an IndexError, since Array does not care whether all the nested Arrays are the same size, as opposed to numpy ndarrays.

Full documentation available here.

Notes

  • Mathematical operations such as addition or multiplication can be done with the add and mul methods, not with the + and * operators to avoid confusion and to retain the behaviour of the built-in list.
  • Inplace operations are postfixed with an underscore (e.g. arr.abs_). However, methods for adding elements to Arrays (append, extend, insert, etc.) are inplace by default. (Note: To be changed. In the next release the operations are inplace if inplace=True is passed to the methods.)
  • Inplace operators are generally faster than out of place operations.
  • Even though Array preserves nearly the same functionality as the built-in list, there are a few differences in their behaviour, the most important of which are
    • == (__eq__) Returns element-wise comparison.
    • bool (__bool__) Returns whether all elements evaluate to True.
    • Arrays are hashable. Note that this is implemented by using the Array's tuple representation in __hash__.
You might also like...
Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021)

Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021) Citation Please cite as: @inproceedings{liu2020understan

[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers
[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Fairseq(-py) is a sequence modeling toolkit that allows researchers and developers to train custom models for translation, summarization, language mod

Sequence-to-Sequence Framework in PyTorch
Sequence-to-Sequence Framework in PyTorch

nmtpytorch allows training of various end-to-end neural architectures including but not limited to neural machine translation, image captioning and au

A highly sophisticated sequence-to-sequence model for code generation

CoderX A proof-of-concept AI system by Graham Neubig (June 30, 2021). About CoderX CoderX is a retrieval-based code generation AI system reminiscent o

Pervasive Attention: 2D Convolutional Networks for Sequence-to-Sequence Prediction

This is a fork of Fairseq(-py) with implementations of the following models: Pervasive Attention - 2D Convolutional Neural Networks for Sequence-to-Se

MASS: Masked Sequence to Sequence Pre-training for Language Generation
MASS: Masked Sequence to Sequence Pre-training for Language Generation

MASS: Masked Sequence to Sequence Pre-training for Language Generation

Sequence-to-Sequence learning using PyTorch

Seq2Seq in PyTorch This is a complete suite for training sequence-to-sequence models in PyTorch. It consists of several models and code to both train

Ptorch NLU, a Chinese text classification and sequence annotation toolkit, supports multi class and multi label classification tasks of Chinese long text and short text, and supports sequence annotation tasks such as Chinese named entity recognition, part of speech tagging and word segmentation.

Pytorch-NLU,一个中文文本分类、序列标注工具包,支持中文长文本、短文本的多类、多标签分类任务,支持中文命名实体识别、词性标注、分词等序列标注任务。 Ptorch NLU, a Chinese text classification and sequence annotation toolkit, supports multi class and multi label classification tasks of Chinese long text and short text, and supports sequence annotation tasks such as Chinese named entity recognition, part of speech tagging and word segmentation.

Code for the paper: Sequence-to-Sequence Learning with Latent Neural Grammars

Code for the paper: Sequence-to-Sequence Learning with Latent Neural Grammars

Sequence to Sequence Models with PyTorch
Sequence to Sequence Models with PyTorch

Sequence to Sequence models with PyTorch This repository contains implementations of Sequence to Sequence (Seq2Seq) models in PyTorch At present it ha

Sequence-to-Sequence learning using PyTorch

Seq2Seq in PyTorch This is a complete suite for training sequence-to-sequence models in PyTorch. It consists of several models and code to both train

Pervasive Attention: 2D Convolutional Networks for Sequence-to-Sequence Prediction

This is a fork of Fairseq(-py) with implementations of the following models: Pervasive Attention - 2D Convolutional Neural Networks for Sequence-to-Se

An implementation of a sequence to sequence neural network using an encoder-decoder
An implementation of a sequence to sequence neural network using an encoder-decoder

Keras implementation of a sequence to sequence model for time series prediction using an encoder-decoder architecture. I created this post to share a

Sequence lineage information extracted from RKI sequence data repo
Sequence lineage information extracted from RKI sequence data repo

Pango lineage information for German SARS-CoV-2 sequences This repository contains a join of the metadata and pango lineage tables of all German SARS-

Official repository of OFA. Paper: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence Learning Framework
Official repository of OFA. Paper: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence Learning Framework

Paper | Blog OFA is a unified multimodal pretrained model that unifies modalities (i.e., cross-modality, vision, language) and tasks (e.g., image gene

A NumPy-compatible array library accelerated by CUDA
A NumPy-compatible array library accelerated by CUDA

CuPy : A NumPy-compatible array library accelerated by CUDA Website | Docs | Install Guide | Tutorial | Examples | API Reference | Forum CuPy is an im

Creates a C array from a hex-string or a stream of binary data.

hex2array-c Creates a C array from a hex-string. Usage Usage: python3 hex2array_c.py HEX_STRING [-h|--help] Use '-' to read the hex string from STDIN.

Comments
  • Feature request: chunks

    Feature request: chunks

    First off, thanks for making this great library!

    What do you think of adding a chunks(n) function that splits an Array into n-sized Arrays?

    Something like..

    >>> Array(range(10)).chunks(5)
    Array(Array(0, 1, 2, 3, 4), Array(5, 6, 7, 8, 9))
    

    I'd be happy to contribute this feature as well.

    opened by mcastorina 6
  • Cannot flatten Array of strings

    Cannot flatten Array of strings

    >>> Array('10', Array('20', '30')).flatten()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      [Previous line repeated 987 more times]
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1007, in flatten
        r = Array(e for s in self for e in (s if isinstance(s, Iterable) else [s]))
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 42, in __init__
        args = list(args[0])
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1007, in <genexpr>
        r = Array(e for s in self for e in (s if isinstance(s, Iterable) else [s]))
      File "/usr/lib/python3.9/abc.py", line 98, in __instancecheck__
        return _abc_instancecheck(cls, instance)
    RecursionError: maximum recursion depth exceeded in comparison
    

    Expected: Array('10', '20', '30') Version: Funct==0.9.2

    opened by mcastorina 1
Releases(v0.9.2)
  • v0.9.2(Feb 3, 2021)

    Release 0.9.2

    • New methods: windows and chunks.
    • inplace keyword for methods (standard for the next release).
    • isFinite returns boolean Array instead of a boolean.
    • Methods with optional keyword arguments as well as "computed" properties i.e. headOption, lastOption, toChar/Int/Str..., (arg)min, (arg)max, any, and all are no longer properties.
    • Warn of bool() of empty Array as it behaves differently from the built-in list.
    • Add FutureWarnings to certain functions as Array is switching to more pythonic naming convention and reserving the underscore postfix for lazy functions in the next release.

    Next release

    • Lazy evaluation.
    • No capital letters in methods.
    Source code(tar.gz)
    Source code(zip)
  • v0.9.1(Jan 10, 2021)

nocasedict - A case-insensitive ordered dictionary for Python

nocasedict - A case-insensitive ordered dictionary for Python Overview Class NocaseDict is a case-insensitive ordered dictionary that preserves the or

PyWBEM Projects 2 Dec 12, 2021
A collection of data structures and algorithms I'm writing while learning

Data Structures and Algorithms: This is a collection of data structures and algorithms that I write while learning the subject Stack: stack.py A stack

Dhravya Shah 1 Jan 09, 2022
Svector (pronounced Swag-tor) provides extension methods to pyrsistent data structures

Svector Svector (pronounced Swag-tor) provides extension methods to pyrsistent data structures. Easily chain your methods confidently with tons of add

James Chua 5 Dec 09, 2022
A Python library for electronic structure pre/post-processing

PyProcar PyProcar is a robust, open-source Python library used for pre- and post-processing of the electronic structure data coming from DFT calculati

Romero Group 124 Dec 07, 2022
Data Structure With Python

Data-Structure-With-Python- Python programs also include in this repo Stack A stack is a linear data structure that stores items in a Last-In/First-Ou

Sumit Nautiyal 2 Jan 09, 2022
Common sorting algorithims in Python

This a Github Repository with code for my attempts for commonly used sorting algorithims, tested on a list with 3000 randomly generated numbers.

Pratham Prasoon 14 Sep 02, 2021
Final Project for Practical Python Programming and Algorithms for Data Analysis

Final Project for Practical Python Programming and Algorithms for Data Analysis (PHW2781L, Summer 2020) Redlining, Race-Exclusive Deed Restriction Lan

Aislyn Schalck 1 Jan 27, 2022
A Python implementation of red-black trees

Python red-black trees A Python implementation of red-black trees. This code was originally copied from programiz.com, but I have made a few tweaks to

Emily Dolson 7 Oct 20, 2022
CLASSIX is a fast and explainable clustering algorithm based on sorting

CLASSIX Fast and explainable clustering based on sorting CLASSIX is a fast and explainable clustering algorithm based on sorting. Here are a few highl

69 Jan 06, 2023
A mutable set that remembers the order of its entries. One of Python's missing data types.

An OrderedSet is a mutable data structure that is a hybrid of a list and a set. It remembers the order of its entries, and every entry has an index number that can be looked up.

Elia Robyn Lake (Robyn Speer) 173 Nov 28, 2022
This repo represents all we learned and are learning in Data Structure course.

DataStructure Journey This repo represents all we learned and are learning in Data Structure course which is based on CLRS book and is being taught by

Aprime Afr (Alireza Afroozi) 3 Jan 22, 2022
IADS 2021-22 Algorithm and Data structure collection

A collection of algorithms and datastructures introduced during UoE's Introduction to Datastructures and Algorithms class.

Artemis Livingstone 20 Nov 07, 2022
One-Stop Destination for codes of all Data Structures & Algorithms

CodingSimplified_GK This repository is aimed at creating a One stop Destination of codes of all Data structures and Algorithms along with basic explai

Geetika Kaushik 21 Sep 26, 2022
Datastructures such as linked list, trees, graphs etc

datastructures datastructures such as linked list, trees, graphs etc Made a public repository for coding enthusiasts. Those who want to collaborate on

0 Dec 01, 2021
A mutable set that remembers the order of its entries. One of Python's missing data types.

An OrderedSet is a mutable data structure that is a hybrid of a list and a set. It remembers the order of its entries, and every entry has an index nu

Elia Robyn Lake (Robyn Speer) 173 Nov 28, 2022
dict subclass with keylist/keypath support, normalized I/O operations (base64, csv, ini, json, pickle, plist, query-string, toml, xml, yaml) and many utilities.

python-benedict python-benedict is a dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, ini, json, pickle, plist, query-string, t

Fabio Caccamo 799 Jan 09, 2023
My notes on Data structure and Algos in golang implementation and python

My notes on DS and Algo Table of Contents Arrays LinkedList Trees Types of trees: Tree/Graph Traversal Algorithms Heap Priorty Queue Trie Graphs Graph

Chia Yong Kang 0 Feb 13, 2022
A simple tutorial to use tree-sitter to parse code into ASTs

A simple tutorial to use py-tree-sitter to parse code into ASTs. To understand what is tree-sitter, see https://github.com/tree-sitter/tree-sitter. Tr

Nghi D. Q. Bui 7 Sep 17, 2022
Python Data Structures and Algorithms

No non-sense and no BS repo for how data structure code should be in Python - simple and elegant.

Prabhu Pant 1.9k Jan 08, 2023
A Munch is a Python dictionary that provides attribute-style access (a la JavaScript objects).

munch munch is a fork of David Schoonover's Bunch package, providing similar functionality. 99% of the work was done by him, and the fork was made mai

Infinidat Ltd. 643 Jan 07, 2023