Python program to get first item of given number in array

WBOY
Release: 2023-08-19 22:13:09
forward
776 people have browsed it

Python program to get first item of given number in array

An array is a data structure that has a set of items of the same data type, with each element identified by an index.

Arrays in python

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

[10, 4, 11, 76, 99]
Copy after login

Python also provides some more suitable modules, these modules are Numpy and array modules.

The integer array defined using the array module is −

array('i', [1, 2, 3, 4])
Copy after login

A Numpy array defined by the NumPy module is −

array([1, 2, 3, 4])
Copy after login

In this article we will see how to get the first item of a given number from an array.

Input and output scenarios

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

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

The first three items 1, 2, and 3 are accessed from the input array. Let's look at an array containing all string elements.

Input array:
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Output:
[‘a’, ‘b’]
Copy after login

First 2items are retrieved from the input array. In the below examples, we will mainly use python slicing features to retrieve the first few elements.

Slicing

The slicing is used to access the group of elements from a sequence. Following is the syntax to perform slicing –

sequence_object[start : end]
Copy after login

Where,

  • Start − The starting index where the slicing of the iterable starts. By default, it is 0.

  • End − The ending index where the slicing of the iterable stops. The default value is the length of the iterable object. And this value is excluded.

Use list

We can use the list-slicing feature to access the first given number of items from an array.

The Chinese translation of

Example

is:

Example

Let us take an example and use list slicing to access the previously given number of elements.

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

print ("The original array is: ", lst) 
print() 
numOfItems = 4
# Get first number of elements
result = lst[:numOfItems]
print ("The first {} number of elements are: {}".format(numOfItems, result))
Copy after login

Output

The original array is:  [1, 2, 0, 4, 1, 2, 3, 8]
The first 4 number of elements are: [1, 2, 0, 4]
Copy after login

The first 4 elements are accessed from the given array using the lst[:numOfItems] syntax and those elements are stored in the result variable.

The Chinese translation of

Example

is:

Example

In this example, we will try to access the exceeded number of elements from an array.

# creating array
lst = [1, 2, 0] 

print ("The original array is: ", lst) 
print() 
numOfItems = 4

# Get first number of elements
result = lst[:numOfItems]
print ("The first {} number of elements are: {}".format(numOfItems, result))
Copy after login

Output

The original array is:  [1, 2, 0]
The first 4 number of elements are: [1, 2, 0]
Copy after login

The requested number of items are more compared to the total number of items available in the array list. Instead of rising an error, the slicing object lst[:numOfItems] displayed the available elements only.

Using NumPy arrays

Like List, we can also use NumPy arrays to access the array elements.

The Chinese translation of

Example

is:

Example

In this example, we will try to use the array slicing function to access the first two elements of a NumPy array.

import numpy

# creating array
numpy_array = numpy.array([1, 3, 5, 6, 2, 9, 8])

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

numOfItems = 2

# get first number of elements
result = numpy_array[:numOfItems]
print ("The result is: ", result) 
Copy after login

Output

The original array is:  [1 3 5 6 2 9 8]
The result is:  [1 3]
Copy after login

The first two elements 1,3 are accessed from the numpy array object.

Using array module

The array module is a Python built-in module used to define array objects using the array() method.

The Chinese translation of

Example

is:

Example

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

import array
# creating array
arr = array.array('i', [2, 1, 4, 3, 6, 5, 8, 7])
print ("The original array is: ", arr) 
print() 
numOfItems = 2
# remove first elements
result = arr[:numOfItems]
print ("The result is: ", result) 
Copy after login

Output

The original array is:  array('i', [2, 1, 4, 3, 6, 5, 8, 7])
The result is:  array('i', [2, 1])
Copy after login

The first 2 elements from the input array arr are stored in the result variable.

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!