Что такое subarray python

Python Program to get the subarray from an array using a specified range of indices

An array is a homogeneous data structure, which is used to store a set of elements of the same data type. And each element in the array is identified by key or index value.

Subarray

A subarray is defined as a small part of continuous elements of an array. if we take an array of 5 integer elements like below.

Then the subarrays will be −

Arrays in Python

In Python we don’t have a specific data structure to represent arrays. However, we can use Lists to represent arrays.

Also we can use array or NumPy modules to work with arrays in python.

An array defined by the array module.

A Numpy array defined by the NumPy module.

The above all arrays are indexed from starting 0 to (n-1).

In this article, we see multiple ways to get the subarray from an array using a specified range of indices

Input Output Scenarios

Assume we have an input array with 9 integer values. And in output will be the subarray from index 1 to 4.

Input array: [1, 2, 3, 4, 5, 6, 7, 8, 9] Output: [2, 3, 4]

Mainly we will use python slicing feature to get the subarray from an array. A slicing used to select a range of items of an iterable.

Syntax

iterable_obj[start:stop:step]

Using List Slicing

We can use list slicing to get the subarray. Following is the syntax to do so −

list_object[start : end : step]
  • Start − The starting index where slicing of the list starts. The default value is 0.
  • End − The ending index where slicing of the list stops. Note that the value of this index is not a part of the end result. The default value is the length of the iterable object.
  • Step − By default, it is 1. The number to increment the starting index.

Example

From the following example, we can see an array and two indices arr[1:7] are given to pick subarrays from those locations.

# creating array arr = [2, 5, 6, 7, 1, 9, 2, 0] print ("The original array is: ", arr) print() # get the subarray result = arr[1:7] print ("The subarray is: ", result)

Output

The original array is: [2, 5, 6, 7, 1, 9, 2, 0] The subarray is: [5, 6, 7, 1, 9, 2]

Using a NumPy Array

In Numpy array slicing allows us to specify a range of indexes to get the subarray from a bigger array. The NumPy array elements are also indexed like a python list. The index of the first element will be 0 and the last element will be indexed n-1.

Syntax

  • Start − The starting index where slicing of the array starts.
  • End − The ending index where slicing of the array stops.

Example

Following is another example –

import numpy as np # creating array arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] print ("The original array is: ", arr) print() # get the subarray result = arr[1:4] print ("The subarray is: ", result)

Output

The original array is: [1, 2, 3, 4, 5, 6, 7, 8, 9] The subarray is: [2, 3, 4]

We successfully got a subarray starting from the 1st index up to the 4th index (excluding the last the 4th index) was selected.

Use the Array Module

The python array module also supports array slicing, so that we can easily get the subarray from a big array. Following is the syntax –

Syntax

  • Start − The starting index where slicing of the array starts.
  • End − The ending index where the slicing of the array stops.

Example

In the following example, we have successfully got the subarray from index 1- 8 using the python array module and its slicing technique.

import array # creating array arr = array.array('i',[1, 2, 3, 4, 5, 6, 7, 8, 9]) print ("The original array is: ", arr) print() # get the subarray result = arr[1:8] print ("The subarray is: ", result)

Output

The original array is: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9]) The subarray is: array('i', [2, 3, 4, 5, 6, 7, 8])

Источник

Get a Subarray of an Array in Python

Get a Subarray of an Array in Python

A contiguous part of an array is a subarray, and a contiguous part of a string is a substring. The order of elements remains the same relative to the original list or string for both of them.

Python makes obtaining a subarray or a substring very straightforward compared to most of the other programming languages.

In this article, we will learn how to obtain a subarray of an array using Python.

Obtain a Subarray of an Array Using Slicing in Python

In Python, we can get a subarray of an array using slicing. Extending indexing, which is an easy and convenient notation, can be used to slice an array or a string. It has the following syntax.

  • object — A list or a string.
  • start — The starting index for slicing. The default value is 0 .
  • end — The ending index for slicing. Note that the value as this index is not a part of the end result. The default value is the length of the iterable object.
  • step — The number of increments between each index in the end result. By default, it is 1 .

Let us understand how to use this Python feature to get a subarray of an array with the help of some examples. Refer to the following code for the same.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = array[1 : 4] b = array[0 : 8] c = array[6 : ] d = array[ : 5] print(a) print(b) print(c) print(d) 
[2, 3, 4] [1, 2, 3, 4, 5, 6, 7, 8] [7, 8, 9, 10] [1, 2, 3, 4, 5] 

As we can see, the output contains all the indexes between the start and the end index ( start inclusive and end exclusive). Moreover, when no value is set for the start , the first index is considered by default, and when no value is set for the end , the last value is considered by default.

We can extend this even further and apply slicing to each individual value of a list. We will create a list of strings and a list of lists and find a substring and a subarray for each string and list using list comprehension and slicing. List comprehension is an inline syntax for iterating over a list or lists and creating a new list.

Refer to the following code for the example discussed above.

a = [  "Hello",  "World",  "Computer",  "Science",  "GitHub",  "StakeOverflow" ] b = [  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],  [1.1, 2.2, 3.3, 4.4, 5.5],  [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],  ["Q", "W", "E", "R", "T", "Y"],  [True, False, None, None, False, True] ] x = [element[1 : 5] for element in a] y = [element[1 : 3] for element in b] print(x) print(y) 
['ello', 'orld', 'ompu', 'cien', 'itHu', 'take'] [[2, 3], [2.2, 3.3], [0.2, 0.3], ['W', 'E'], [False, None]] 

For each string in the list of strings, the above Python code concatenates characters present at indexes 1 , 2 , 3 , and 4 into a new string and creates a new list of strings. And for the list of lists, it clubs together all the values at indexes 1 and 2 and creates a new list of lists. Furthermore, we store both the new lists in two new variables and print them.

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article — Python Array

Источник

Difference between Subarray, Subsequence, and Subset

This post will discuss the difference between a subarray, a substring, a subsequence, and a subset.

1. Subarray

A subarray is a slice from a contiguous array (i.e., occupy consecutive positions) and inherently maintains the order of elements. For example, the subarrays of array are , , , , , and .

Following is the C, Java, and Python program to generate all subarrays of the specified array:

C

Java

Python

Please note that there are precisely n×(n+1)/2 subarrays in an array of size n . Also, there is no such thing as a contiguous subarray. The prefix contiguous is sometimes applied to make the context more clear. So, a contiguous subarray is just another name for a subarray.

2. Substring

A substring of a string s is a string s’ that occurs in s . A substring is almost similar to a subarray, but it is in the context of strings.

For example, the substrings of string ‘apple’ are ‘apple’, ‘appl’, ‘pple’, ‘app’, ‘ppl’, ‘ple’, ‘ap’, ‘pp’, ‘pl’, ‘le’, ‘a’, ‘p’, ‘l’, ‘e’, » . Following is the C++, Java, and Python program that generates all non-empty substrings of the specified string:

C++

Output:

't', 'te', 'tec', 'tech', 'techi', 'techie', 'e', 'ec', 'ech', 'echi', 'echie', 'c', 'ch', 'chi', 'chie', 'h', 'hi', 'hie', 'i', 'ie', 'e'

Java

Output:

't', 'te', 'tec', 'tech', 'techi', 'techie', 'e', 'ec', 'ech', 'echi', 'echie', 'c', 'ch', 'chi', 'chie', 'h', 'hi', 'hie', 'i', 'ie', 'e'

Python

Output:

t te tec tech techi techie e ec ech echi echie c ch chi chie h hi hie i ie e

3. Subsequence

A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, is a subsequence of sequence obtained after removing and .

People are often confused between a subarray/substring and a subsequence. A subarray or substring will always be contiguous, but a subsequence need not be contiguous. That is, subsequences are not required to occupy consecutive positions within the original sequences. But we can say that both contiguous subsequence and subarray are the same.

In other words, the subsequence is a generalization of a substring, or substring is a refinement of the subsequence. For example, is a subsequence of , but not a substring, and is both a subarray and a subsequence.

Please note that a subsequence can be in the context of both arrays and strings. Generating all subsequences of an array/string is equivalent to generating a power set of an array/string. For a given set, S , we can find the power set by generating all binary numbers between 0 and 2 n -1 , where n is the size of the given set. This approach is demonstrated below in C++, Java, and Python:

Источник

Subarrays, Subsequences, and Subsets in Array

A subarray is a contiguous part of array, i.e., Subarray is an array that is inside another array.

In general, for an array of size n, there are n*(n+1)/2 non-empty subarrays.

For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are:

(1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4), and (1,2,3,4)

What is a Subsequence?

A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.

More generally, we can say that for a sequence of size n, we can have (2 n – 1) non-empty sub-sequences in total.

For the same above example, there are 15 sub-sequences. They are:

(1), (2), (3), (4), (1,2), (1,3),(1,4), (2,3), (2,4), (3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4), (1,2,3,4).

What is a Subset?

If a Set has all its elements belonging to other sets, this set will be known as a subset of the other set.

A Subset is denoted as ““. If set A is a subset of set B, it is represented as A ⊆ B.

For example, Let Set_A = , Set_ B =

Источник

Читайте также:  Type casting objects in java
Оцените статью