Home >Backend Development >Python Tutorial >Python program to get last given number of items in array

Python program to get last given number of items in array

王林
王林forward
2023-09-03 12:29:16649browse

Python program to get last given number of items in array

An array is a data structure composed of many elements of the same data type, each element identified by an index.

[2, 4, 0, 5, 8] 

Arrays in Python

Python does not have its own data structure to represent arrays. However, we can use list data structures as an alternative to arrays. Here we will use a list as an array:

[10, 4, 11, 76, 99]

Python provides some modules to handle arrays more appropriately, they are Numpy and the array module.

In this article we will see different ways to access the last given number of elements from an array.

Input and output scenarios

Suppose we have an input array containing 9 integer values. In the output, the last few items are accessed based on the specified number.

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

Access the last 3 items 7, 8, and 9 from the input array.

Input array:
[10, 21, 54, 29, 2, 8, 1]
Output:
[29, 2, 8, 1]

Retrieve the last 4 items from the input array.

In the following example, we will primarily use Python's negative indexing and slicing capabilities to retrieve the last few elements.

Negative Indexing in Python

Python also supports negative indexing, which means counting elements with a negative sign starting from the end of the array, and starting from 1 instead of starting from 0.

[1, 2, 3, 4, 5]
-5 -4 -3 -2 -1

The first element is identified by the index value -n, and the last element is -1.

Slices in Python

Using the slicing function in Python, a set of elements can be accessed from a sequence with the shortest possible syntax.

grammar

sequence_object[start : end : step]
  • Start: The starting index of the slice, the starting position of the slice of the iterable object, the default is 0.

  • End: The end index where the slice list stops. The default value is the length of the iterable. and the value is excluded.

Use list

By using the list slicing function, we can access the last given number of elements in the array.

Example

Let's take an example of applying list slicing to access the last few elements in an array.

# creating array
lst = [1, 2, 0, 4, 2, 3, 8] 

print ("The original array is: ", lst) 
print() 

numOfItems = 4

# Get last number of elements
result = lst[-numOfItems:]
print ("The last {} number of elements are: {}".format(numOfItems, result))

Output

The original array is:  [1, 2, 0, 4, 2, 3, 8]

The last 4 number of elements are: [4, 2, 3, 8]

Use negative indices to access the last 4 elements from the given array.

Using NumPy arrays

Let us use a NumPy array to access the last given number of elements.

Example

In this example, we will access numpy array elements with the help of negative index values.

import numpy
# creating array
numpy_array = numpy.random.randint(1, 10, 5)
print ("The original array is: ", numpy_array) 
print() 
numOfItems = 2
# get the last element
result = numpy_array[-numOfItems:]
print ("The last {} number of elements are: {}".format(numOfItems, result))

Output

The original array is:  [4 6 9 7 5]
The last 2 number of elements are: [7 5]

We have successfully accessed the last 2 elements in the NumPy array. Element 7 is indexed with -2 and element 5 is indexed with -1.

Using the array module

By using the array() method, we will create an array of a specific data type.

Example

In this example, we will create an array using the array module.

import array
# creating array
arr = array.array('i', [6, 5, 8, 7])
print ("The original array is: ", arr) 
print() 
numOfItems = 2
# remove last elements
result = arr[-numOfItems:]
print ("The last {} number of elements are: {}".format(numOfItems, result))

Output

The original array is:  array('i', [6, 5, 8, 7])
The last 2 number of elements are: array('i', [8, 7])

From the above example, the requested number of items has been successfully accessed. Python slicing does not generate any errors if the number of elements requested exceeds the total number of elements in the sequence.

The above is the detailed content of Python program to get last given number of items in array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Python random moduleNext article:Python random module