Repeat all the elements of a NumPy array of strings
Last Updated :
05 Mar, 2023
Let us see how to repeat all elements of the given array of string 3 times. Example :
Input : ['Akash', 'Rohit', 'Ayush', 'Dhruv', 'Radhika'] Output : ['AkashAkashAkash', 'RohitRohitRohit', 'AyushAyushAyush', 'DhruvDhruvDhruv', 'RadhikaRadhikaRadhika']
We will be using the numpy.char.multiply(a, i) method for this task.
numpy.char.multiply(a, i)
Syntax : numpy.char.multiply(a, i) Parameters :
- a : array of str or unicode
- i : number of times to be repeated
Returns : Array of strings
Example 1 : Repeating 3 times.
Python3
# importing the module
import numpy as np
# created array of strings
arr = np.array(['Akash', 'Rohit', 'Ayush',
'Dhruv', 'Radhika'], dtype = np.str)
print("Original Array :")
print(arr)
# with the help of np.char.multiply()
# repeating the characters 3 times
new_array = np.char.multiply(arr, 3)
print("\nNew array :")
print(new_array)
Output :
Original Array : ['Akash' 'Rohit' 'Ayush' 'Dhruv' 'Radhika'] New array : ['AkashAkashAkash' 'RohitRohitRohit' 'AyushAyushAyush' 'DhruvDhruvDhruv' 'RadhikaRadhikaRadhika']
Time complexity: O(n), where n is the length of the input array
Auxiliary space: O(n).
Example 2: Repeating 2 times.
Python3
# importing the module
import numpy as np
# created array of strings
arr = np.array(['Geeks', 'for', 'Geeks'])
print("Original Array :")
print(arr)
# with the help of np.char.multiply()
# repeating the characters 3 times
new_array = np.char.multiply(arr, 2)
print("\nNew array :")
print(new_array)
Output :
Original Array :
['Geeks' 'for' 'Geeks']
New array :
['GeeksGeeks' 'forfor' 'GeeksGeeks']
Time complexity: O(n), where n is the number of characters in the original array.
Auxiliary space: O(n), where n is the number of characters in the original array.
EXAMPLE 3 - Method 2: We can use a list comprehension to repeat each string in the array multiple times .
Python3
# importing the module
import numpy as np
# created array of strings
arr = np.array(['Geeks', 'for', 'Geeks'])
print("Original Array :")
print(arr)
# using list comprehension to repeat each string in the array
n = 2 # number of times to repeat each string
new_array = np.array([s * n for s in arr])
print("\nNew array :")
print(new_array)
OUTPUT-
Original Array :
['Geeks' 'for' 'Geeks']
New array :
['GeeksGeeks' 'forfor' 'GeeksGeeks']
Time complexity: O(n * k), where n is the length of the input array and k is the number of times each string is repeated.
Auxiliary space: O(n), as we create a new list of length n to store the repeated strings. However, this is not counting the space needed for the input and output arrays.
Similar Reads
Python | dtype object length of Numpy array of strings In this post, we are going to see the datatype of the numpy object when the underlying data is of string type. In numpy, if the underlying data type of the given object is string then the dtype of object is the length of the longest string in the array. This is so because we cannot create variable l
3 min read
Element-wise concatenation of two NumPy arrays of string In this article, we will discuss how to concatenate element-wise two arrays of string Example : Input : A = ['Akash', 'Ayush', 'Diksha', 'Radhika'] B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal'] Output : A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal'] We will be using the numpy.cha
2 min read
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read
Modify Numpy array to store an arbitrary length string NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). The dtype of
4 min read
How to split the element of a given NumPy array with spaces? To split the elements of a given array with spaces we will use numpy.char.split(). It is a function for doing string operations in NumPy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr. Parameters:arr : array_like of str or unicode.Input array
2 min read