Find the length of each string element in the Numpy array in C++

C++Server Side ProgrammingProgramming

Here we will see how to get the length of each string element in the Numpy Array. Numpy is a library for Numeric Python, and it has very powerful array class. Using this we can store data in an array like structure. To get the length we can follow two different approach, these are like below −

Example

import numpy as np
str_arr = np.array(['Hello', 'Computer', 'Mobile', 'Language', 'Programming', 'Python'])
print('The array is like: ', str_arr)
len_check = np.vectorize(len)
len_arr = len_check(str_arr)
print('Respective lengts: ', len_arr)

Output

The array is like: ['Hello' 'Computer' 'Mobile' 'Language' 'Programming' 'Python']
Respective lengts: [ 5 8 6 8 11 6]

Another approach using loop

Example

import numpy as np
str_arr = np.array(['Hello', 'Computer', 'Mobile', 'Language', 'Programming', 'Python'])
print('The array is like: ', str_arr)
len_arr = [len(i) for i in str_arr]
print('Respective lengts: ', len_arr)

Output

The array is like: ['Hello' 'Computer' 'Mobile' 'Language' 'Programming' 'Python']
Respective lengts: [5, 8, 6, 8, 11, 6]
raja
Published on 04-Nov-2019 07:52:59
Advertisements